120 likes | 280 Views
Storing Integers (c data type int ). RAM Allocation. Integers require 2 CONTIGUOUS bytes (16-bits) of storage. Consider the c declaration:. int x = ‘W’ , y , z = 5487 ;. In Fact we are (once again): 1. Requesting 6-bytes (2 per variable) of RAM be reserved
E N D
Storing Integers (c data type int) RAM Allocation Integers require 2 CONTIGUOUS bytes (16-bits) of storage Consider the c declaration: intx = ‘W’, y, z = 5487; In Fact we are (once again): 1. Requesting 6-bytes (2 per variable) of RAM be reserved 2. Associating each variable name with a reserved location (LOCATIONSx, y, and z) 3. Initializing location x with ‘W’ (= ASCII 87 = 10101112= 00000000010101112 on 16-bits) 4. Initializing location z with 5487 (548710 = 10101011011112 = 00010101011011112 on 16-bits)
Let’s Assume locations 7212, 7213, 7214, 7216, and 7219 through 7250 Are available: RAM Allocation • Variable x is assigned address 7212 (and 7213) • Variable y is assigned address 7219 (and 7220) • Variable z is assigned address 7221 (and 7222) Why aren’t addresses 7214 and 7216 used??? Because we need 2 contiguous bytes of storage for integers: • We can’t use location 7214 because location 7215 is NOT available • We can’t use location 7216 because location 7217 is NOT available Looking at RAM, We might see:
7220 (y) 7217 7216 7222 (z) 7215 7214 7223 7213 (x) 7226 7212 (x) 7224 7211 7221 (z) 7225 7219 (y) 7218 11100001 00011001 00101001 00000000 01010111 00100111 00000100 01110011 01100110 00000000 000101010 00000000 10010010 00100010 01101111 00110110 intx = ‘W’,y,z= 5487; x = 0000000001010111 y is unassigned z = 0001010101101111 RAM Allocation • x stored at address 7212 (and 7213) • y stored at address 7219 (and 7220) • z stored at address 7221 (and 7222)
00011001 01100110 7219 7220 Once again, Notice that there is ‘garbage’ in location y (Addresses 7219 & 7220): RAM Allocation = 212 + 211 + 28 + 26 + 25 + 22 + 21 = 4096 + 2048 + 256 + 64 + 32 + 4 + 2 = 6,502 Which is the output we would obtain if we issued the command: printf (“%d”, y);
What would happen if we tried to print an integer as as an ASCII character ??? RAM Allocation Depends: intanumber =104; printf (“%c”, anumber); • If we issue the commands: h The output will be: Which is the ASCII character for decimal value 104 intanumber =6502; printf (“%c”, anumber); • If we issue the commands: The output will be: f WHAT ???
Don’t forget how the integer 6502 was stored (on 16-bits): RAM Allocation 0 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 When we try to print out a character according to the ASCII tables, we consider ONLY 8-bits (the right-most 8-bits) = 26 + 25 + 22 + 21 = 64 + 32 + 4 + 2 = 102 Which corresponds to the ASCII character ‘f’
What if an illegal value is entered?? The same situation occurs as did when we entered an illegal value for the data type char: RAM Allocation intbadnumber =-52434; If we make the declaration: Since: 52,43410= 11001100110100102 Then: - 52,43410= 00110011001011012 1’s Comp. + 1 0011001100101110 2’s Comp. = 213 + 212 + 29 + 28 + 25 + 23 + 22 + 21 = 8192 + 4096 + 512 + 256 + 32 + 8 + 4 + 2 = 13,102 Which is the output produced by the Statement: printf (“%d”, badnumber);
One more quick example: RAM Allocation Suppose we make the declaration: intbadnumber =72925; Requiring 17-bits Where: 7292510 = 100011100110111012 Since an integer is stored on 16-bits: = 212 + 211 + 210 + 27 + 26 + 24 + 23 + 22 + 20 = 4096 + 2048 + 1024 + 128 + 64 + 16 + 8 + 4 + 1 = 7,389 Which is the output produced by the Statement: printf (“%d”, badnumber);
Other Integers: RAM Allocation Data Type Bits Required Legal Values unsigned int 16 (2-bytes) 0 through 65,535 (No sign bit) 32 (4-bytes) long Or signed long -2,147,483,648 through 2,147,483,647 32 (4-bytes) 0through 4,294,967,295 unsigned long How would the data type long appear in RAM ???
This Concludes The Slides for this Section Choose an Option: Repeat Slides for this Section Go To Next Set of Slides For this Chapter Go To Slide Index For Chapter 3 Go To Slide Index For Chapter 4 Go To Slide Index For Textbook Go To Home Page