wpe41.gif (23084 bytes)CIS3355: Business Data Structures
Fall, 2008
 

How are Integers Stored in RAM?

 

What you must know before we start:

bullet

Random Access Memory is where the computer stores temporary data during program execution.

                                                 

bullet

Integers require 2-bytes or 16 bits of storage each.

bullet

Integers are signed by default.

bullet

Integer variables hold values that have no fractional part (that is, whole numbers only).

bullet

Signed integers allow the storage of all values from -32,768 to 32,767.

bullet

Unsigned integers allow the storage of all values from 0 to 65,535

bullet

signed integer variables can hold positive or negative values, whereas unsigned integer variables can hold only positive values (and 0).

 

Your computer's memory can be viewed as a series of cubbyholes. Each cubbyhole is one of many such holes all lined up. Each cubbyhole or memory location is numbered sequentially. These numbers are known as memory addresses. A variable reserves one or more addresses in which a binary value is stored. Each address is one byte (8 bits) large.

                                            

Now, we are ready to start, and to introduce our explanation of how integers are stored in RAM lets begin by analyzing the following c program declaration:

                                int  x = "J", y = 1145, z;

bullet

First we are declaring the variables x, y and z as integers which means that we are asking for 2-bytes of storage for each or for a total of 6 bytes.

bullet

We are also initializing the variable x with the character J & the variable y with the decimal value 1145.

 

When the previous declaration is enter the program looks for available space to store the data. 

                            BUT WHERE ARE THESE VALUES GOING TO BE STORED??????????

Well, it all depends on where there is space available.  However, with integers you have to keep in mind that they have to be stored in 2 contiguous bytes of memory.  It is not possible to store part of an integer in for example address 1245 and the rest in address 1520.

 

If we where to look inside RAM we would see something similar to this:

 

1245 1246 1247 1248 1249 1250 1251
00000000 01001010 00011010 11000010 10010010 00000100 01111001
1252 1253 1254 1255 1256 1257 1258
00011010 01000010 00010111 11101110 11010000 00001101 01001010

 

  1. The variable x is stored in address 1245 & 1246.

  2. The variable y is stored in address 1250 & 1251.

  3. The variable z is stored in address 1254 & 1255.

                BUT WHY IS THERE A VALUE IN ADDRESSES 1254 & 1255 IF WE HAVE NOT STORED                          ANYTHING IN THERE????

Well, very simple that is because in c data is stored temporarily but when it is used the locations are not resetted to zero instead they keep the information that was previously stored until you allocate new values into the locations.  That means that if we were to print the contents of this two locations it would print whatever information was stored there before.

 

    You may have notice that variable x contains a character which only needs 7 bits of storage.  But, how can it be stored as an integer??? 

The only difference is that integers require 16 bits of storage so in order to store a character as an integer we would need to add zeros before the fist number one until we complete the 16 bits.  For example in this case the character J is stored as 00000000 01001010.  In the same way we can print out an integer as a character in doing so we would look at the rightmost 8 bits, determine the numeric value stored there and the print out the corresponding ASCII character. 

 

 

What would happen if we assign a value like 55,456 to an integer variable???????????

55, 456 is an illegal value because remember that integers only take from the range of values -32,768 to 32,767.  However, c is a program that even when given an illegal value it will perform an operation.  In this situation the program will do the following:

  1.  First it convert the decimal value into binary:

                55,456  =  1101100010100000

  2.    Then it will interpret that number.  First, since the first number is 1 the number appears to be negative.

  3.  Then to evaluate we take the two's complemented number:

                101100010100000

                010011101011111

            +                            1

                010011101100000           

  4.  Then we determine the value of 10011101100000 to be -10,080.

 

If we really need to store integer values such as the one used in our previous example or even greater than that c programming language allows for the use of additional integer data type:

UNSIGNED INTEGERS; which allow to store values form 0 to 65,535.

LONGS; which allow to store values from -2,147,483,648 to 2,147,483,647.

UNSIGNED LONGS; which allow to store values from 0 to 4,294,967,296.

These additional types of integers are stored the same as regular integers, with the only difference that with unsigned integers and longs you don't have to worry about complementing.

 

USEFUL INFORMATION

Variable Types.

Type Size Values
unsigned short int 2 bytes 0 to 65,535
short int 2 bytes -32,768 to 32,767
unsigned long int 4 bytes 0 to 4,294,967,295
long int 4 bytes -2,147,483,648 to 2,147,483,647
int (16 bit) 2 bytes -32,768 to 32,767
int (32 bit) 4 bytes -2,147,483,648 to 2,147,483,647
unsigned int (16 bit) 2 bytes 0 to 65,535
unsigned int (32 bit) 2 bytes 0 to 4,294,967,295
char 1 byte 256 character values
float 4 bytes 1.2e-38 to 3.4e38
double 8 bytes 2.2e-308 to 1.8e308

 

 

******TEST YOUR KNOWLEDGE******

(Answers at bottom of the page)

Multiple choice questions

1.  If locations 2653, 2654, 2655 and 2656 are the only locations we can use, and location 2654 contains the numeric

     value 100. If I requested unsigned int a='A', What would happen?

        a) use location 2653 and 2655 because integers need two bytes, location 2654 is being used.

        b) use 2653, clear location 2654 and use them both to store the integer.

        c) It cannot be done because integers need two contiguous bytes and 'A' is a character which means that it only

             needs one by of storage.

        d) use 2655 and 2656 and store the binary

            number 0000 0000 0100 0001

 

2.  Regardless whether signed or unsigned, the data type int requires ___________ of storage per variable.

        A) Two bytes

        B) Three bytes

        C) Sixteen bits

D) A and C

        E) None of the above

 

3  If in the binary values 00011010 and 00111111 are stored in the addresses 2567 and 2568 correspondingly, what

    decimal value is stored at address 2568 if the data type is an integer?

         a) 63 because this is the ASCII decimal value associated with the binary value 00111111.

         b) -65

         c) 6719 because integers require 2 bytes of contiguous memory, so both addresses 2567 and 2568 would have to

            be evaluated in order to determine the answer.

         d) None of the above, we do not have enough information to determine the answer.

Short answer questions

 1.  What is the difference between storing a character versus an integer in RAM?

 2.  Can an integer be stored in any available space in RAM?

 3.  How is a variable address referred to in RAM?

 4.  What happens if I assign a negative number to an unsigned variable? Consider the following line of code:

unsigned int aPositiveNumber = -1;

 

RELATED LINKS AND REFERENCES:

http://www.tvcc.cc/staff/fuller/cs160/chap1/chap1.html

http://s56.net/C/ch03/ch03.htm

http://en.wikipedia.org/wiki/Integer_%28computer_science%29

Answers to Test Your Knowledge Questions.

Multiple choice questions

1. d

2. d

3. c

Short answer questions

1.  An integer is stored in the same manner as a character in RAM.  An integer however requires more space 2 or 4 bytes depending on machine as well as contiguous memory to accommodate the integer value of the variable.

2.  Yes and NO, Integers can be stored in any available space as long as the addresses in memory are contiguous from the beginning byte to ending byte.

3.  A variable address is referred to as the base location of the variable.  If you have an integer that is occupying addresses 100 & 101 the variable address would be 100.

4.  The negative number will be assessed as a bit pattern and assigned to the variable. The value of that variable will then be interpreted as an unsigned number. Thus, -1, whose bit pattern is 11111111 11111111 (0xFF in hex), will be assessed as the unsigned value 65,535.