160 likes | 321 Views
Number Representation. Lecture 20 4.3.2001. Topics. How are numeric data items actually stored in computer memory? How much space (memory locations) is allocated for each type of data? int, float, char, etc. How are characters and strings stored in memory?. Binary number system.
E N D
Number Representation Lecture 20 4.3.2001.
Topics • How are numeric data items actually stored in computer memory? • How much space (memory locations) is allocated for each type of data? • int, float, char, etc. • How are characters and strings stored in memory?
Binary number system Decimal number system • Ten digits : 0,1,2,3,4,5,6,7,8,9 • Every digit position has a weight which is a power of 10. • Base or radix is 10. • Two digits : 0,1 • Every digit position has a weight which is a power of 2. • Base or radix is 2.
Decimal Number • 136.25 : What does this number actually mean ? 102 * 1 = 100.0 101 * 3 = 30.0 100 * 6 = 6.0 10-1 * 2 = 0.2 10-2 * 5 = 0.05
Binary Number • 1101.01: What does this number mean? 23 * 1 = 1000.0 (8 in decimal) 22 * 1 = 100.0 (4 in decimal) 21 * 3 = 0.0 (0 in decimal) 20 * 6 = 1.0 (1 in decimal) 2-1 * 2 = 0.0 (0.0 in decimal) 2-2 * 5 = 0.01 (0.25 in decimal)
First integers and their binary equivalent decimal binary 0 0000 (0*23 + 0*22 + 0*21 + 0*20) 1 0001 (0*23 + 0*22 + 0*21 + 1*20) 2 0010 (0*23 + 0*22 + 1*21 + 0*20) 3 0011 (0*23 + 0*22 + 1*21 + 1*20) 4 0100 (0*23 + 1*22 + 0*21 + 0*20) 5 0101 (0*23 + 1*22 + 0*21 + 1*20) 6 0110 (0*23 + 1*22 + 1*21 + 0*20) 7 0111 (0*23 + 1*22 + 1*21 + 1*20) 8 1000 (1*23 + 0*22 + 0*21 + 0*20) 9 1001 (1*23 + 0*22 + 0*21 + 1*20)
Adding Binary Numbers • Basic Rules: • 0+0=0 • 0+1=1 • 1+0=1 • 1+1=0 (carry 1) • Example: 01101001 00110100 ------------- 10011101
Weighted number systems • N = Mj=0bj Bj • N: the number • M : Number of digits • b: The digit • B : System’s radix
Examples 1. 101011 1x25 + 0x24 + 1x23 + 0x22 + 1x21 + 1x20 = 43 (101011)2 = (43)10 2. .0101 0x2-1 + 1x2-2 + 0x2-3 + 1x2-4 = .3125 (.0101)2 = (.3125)10 3. 101.11 1x22 + 0x21 + 1x20 + 1x2-1 + 1x2-2 5.75 (101.11)2 = (5.75)10
Decimal-to-Binary Conversion • Consider the integer and fractional parts separately. • For the integer part, • Repeatedly divide the given number by 2, and go on accumulating the remainders, until the number becomes zero. • Arrange the remainders in reverse order. • For the fractional part, • Repeatedly multiply the given fraction by 2. • Accumulate the integer part (0 or 1). • If the integer part is 1, chop it off. • Arrange the integer parts in the order they are obtained.
Example 1 :: 239 • 239 • 2 119 --- 1 • 59 --- 1 • 2 29 --- 1 • 14 --- 1 • 2 7 --- 0 • 3 --- 1 • 2 1 --- 1 • 2 0 --- 1 (239)10 = (11101111)2
Example 2 :: 64 • 64 • 2 32 --- 0 • 16 --- 0 • 2 8 --- 0 • 4 --- 0 • 2 2 --- 0 • 1 --- 0 • 2 0 --- 1 (64)10 = (1000000)2
Example 3: .634 .634 x 2 = 1.268 .268 x 2 = 0.536 .536 x 2 = 1.072 .072 x 2 = 0.144 .144 x 2 = 0.288 : : (.634)10 = (.10100……)2
Example 4: 37.0625 (37)10 = (100101)2 (.0625)10 = (.0001)2 (37.0625)10 = (100101.0001)2
Hexadecimal Numbers • Base=16 Decimal Binary Hex 0 00000 0 1 00001 1 2 00010 2 3 00011 3 4 00100 4 5 00101 5 6 00110 6 7 00111 7 8 01000 8 9 01001 9 Decimal Binary Hex 10 01010 10 11 01011 11 12 01100 12 13 01101 13 14 01110 14 15 01111 15 16 00110 16 17 00111 17 18 01000 18 19 01001 19