CS255 Syllabus

### What are those bits in memory ???

• Review: information encoding

• We have seen that there are many different kinds of information stored in the computer memory:

• integer (= whole number) values:

 ``` 00000000000000000000000000000101 represents 510 01000000101000000000000000000000 represents 108422758410 (= 230 + 223 + 221) ```

• Floating point values:

 ``` 01000000101000000000000000000000 represents 510 See: click here ```

• Character:

 ``` 00110101 represents the character 5 ```

• What does these bits represent ???

• \$64,000 question:

• Suppose you see the following bit pattern stored in memory:

 ``` 01000000101000000000000000000000 ```

Question:

 What value does this bit pattern encode ??? Does 01000000101000000000000000000000 represent 108422758410 ??? Or does 01000000101000000000000000000000 represent 510 ??? Or something else ???

Answer:

• You cannot tell what value a bit pattern will represent by looking only at the bit pattern

• To decode the information that is represented by a bit pattern, you need this information:

 The encoding method used by the bit pattern (The encoding method is uniquely tied to the data type --- so knowing the data type will tell you the encoding method used)

Example:

 ``` 01000000101000000000000000000000 as an integer value represents: 108422758410 01000000101000000000000000000000 as a floating point value represents: 510 01000000101000000000000000000000 as character (ASCII) code represents: 01000000 10100000 00000000 00000000 '@' unprint NUL NUL And so on !!! (If there are more encoding methods) ```

• Illustration to help you overcome this weirdness

• Weirdness:

• The same bit pattern can represent different value !!!

Example:

 ``` 01000000101000000000000000000000 as an integer value represents: 108422758410 01000000101000000000000000000000 as a floating point value represents: 510 ```

Let me use the English language to help you overcome this weirdness....

• Question:

• Make a correct English sentence that starts with:

 ``` You is ...... ```

You (as a human) is so conditioned to use "you" in the context of a personal pronounce (the "me"-generation !), that you don't even think about the fact that:

 The word "you" can also be used in another context

Example:

 ``` You is an English word ```

• So:

 The same word "you" can have different meaning in different context !!! (This example is completely different from homonyms --- like "bow" (bow down and bow of a ship) !!! Homonyms are different words with the same spelling)

Just like:

 ``` 01000000101000000000000000000000 as an integer value represents: 108422758410 01000000101000000000000000000000 as a floating point value represents: 510 ```

I'll tell you a joke about Millie and her teacher with the word "I" in class...

• What information is stored in memory ???

• Recall:

 Each memory cell stores 8 (eight) bits (There are no boundary or markers in memory that tells you when to stop !!!)

• \$64,000 question:

• Suppose you see the following memory content at address 2000:

Question:

 What possible values are stored in memory location 2000 ???

The answer will surprise you !!!

Answer: the possible values stored at memory address 2000 are

1. The float value 510:

2. The int value 108422758410:

3. The short value 1657610:

4. The byte value 6410:

5. The character value '@':

6. And so on !!!! (if you have more codes !!!)

Explanation:

• If the type of the value stored at memory address is float, then:

 You must take 4 bytes (because a float representation is 32 bits) and use IEEE 754 code to interpret the value

• If the type of the value stored at memory address is int, then:

 You must take 4 bytes (because a int representation is 32 bits) and use 2's complement code to interpret the value

• If the type of the value stored at memory address is short, then:

 You must take 2 bytes (because a short representation is 16 bits) and use 2's complement code to interpret the value

• If the type of the value stored at memory address is byte, then:

 You must take 1 byte (because a byte representation is 8 bits) and use 2's complement code to interpret the value

• If the type of the value stored at memory address is char, then:

 You must take 1 byte (because a char representation is 8 bits) and use ASCII code to interpret the value

• And so on !!! (for different data types)

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   gcc ShowValueDepOnType2.c To run:          a.out

• Summary: to know what value is stored in some variable in memory, you need:

 The memory address where the variable is stored (allocated) in memory Also: the data type of the variable !!!