Dr. Shvetha Soundararajan shvetha@gmu.edu Material adapted from Chapter 2 of Computer Science Illuminated, 7th Edition NUMBERS Natural Numbers Zero and any number obtained by repeatedly adding one to it Integers A natural number, a negative number Examples: 100, 0, 45645, 32 Examples: 249, 0, –45645, –32 Negative Numbers Rational Numbers A value less than 0, with a – sign Examples: –24, –1, –45645, –32 An integer or the quotient of two integers Examples: –249, –1, 0, 3/7, –2/5 NUMBER REPRESENTATION There is only one set of integers, no matter what you call them. | 5 = V = 1012 = |||| Each base gives a different perspective on how to name these numeric values. NUMBER REPRESENTATION § Decimal | 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,… § Binary 0,1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1 111,10000,… § Hexadecimal 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,… § Roman I,II,III,IV,V,VI,VII,VIII,IX,X,XI,XII,XIII,XIV,XV,XVI,XVII,XVIII,XIX,XX,XXI,... | | |||, |||| | ||, |||| | ||||, § Tallies: |, ||, |||, ||||, ||||, |||| |, |||| | ||||, | … |||| DIFFERENT BASES § A base defines how many symbols we have for numbers, and also defines the value of each column. § Those symbols are used "odometer-style" to count upwards. DIFFERENT BASES § We count in decimal (base 10), thanks to our ten fingers. → Base 10: ten symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. § Computers store everything in binary (base 2), thanks to the simplicity of having just 2 states. → Base 2: two symbols: 0, 1. DIFFERENT BASES § We use hexadecimal (base 16) as a convenient shorthand for longer binary numbers. → Base 16: 16 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. § Base N: use any N symbols. Example: 0,1,…, (n-1). COUNTING UP § We count up though the symbols of our base, and when we run out, we 'clock over' to the next column, starting over in the earlier columns. § It just happens a lot faster in binary… § Notice that binary clocks over into 4 new columns exactly when hexadecimal clocks over once. Not a coincidence! reason: 16 = 24. § Think of counting in each base as using an odometer that has a different number of symbols on its wheels. Base 10: Base 2: Base 16: Base 8: 0 0 0 0 1 1 1 1 2 10 2 2 3 11 3 3 4 100 4 4 5 101 5 5 6 110 6 6 7 111 7 7 8 1000 8 10 9 1001 9 11 10 1010 A 12 11 1011 B 13 12 1100 C 14 13 1101 D 15 14 1110 E 16 15 1111 F 17 16 10000 10 20 17 10001 11 21 18 10010 12 22 19 10011 13 23 … … … … POSITIONAL NOTATION How many ones are there in 642? 600 + 40 + 2 ? Or is it 384 + 32 + 2 ? Or maybe… 1536 + 64 + 2 ? 642 is 600 + 40 + 2 in BASE 10 The base of a number determines the number of different digit symbols (numerals) and the values of digit positions. POSITIONAL NOTATION Continuing with our example… 642 in base 10 positional notation is: 6 x 102 = 6 x 100 = 600 + 4 x 101 = 4 x 10 = 40 + 2 x 10º = 2 x 1 = 2 = 642 in base 10 This number is in base 10 The power indicates the position of the number POSITIONAL NOTATION R is the base of the number As a formula: dn * Rn - 1 + dn - 1 * Rn - 2 + ... + d2 * R1 + d1 * R0 n is the number of digits in the number 642 is 6 * 102 + 4 * 10 + 2 * 1 d is the digit in the ith position in the number POSITIONAL NOTATION What if 642 has the base of 13? 6 x 132 = 6 x 169 = 1014 + 4 x 131 = 4 x 13 = 52 + 2 x 13º = 2 x 1 = 2 = 1068 in base 10 642 in base 13 is equivalent to 1068 in base 10 CONVERTING BINARY TO DECIMAL What is the decimal equivalent of the binary number 1101110? 1 x 26 + 1 x 25 + 0 x 24 + 1 x 23 + 1 x 22 + 1 x 21 + 0 x 2º = = = = = = = 1 x 64 1 x 32 0 x 16 1x8 1x4 1x2 0x1 = 64 = 32 =0 =8 =4 =2 =0 = 110 in base 10 CONVERTING OCTAL TO DECIMAL What is the decimal equivalent of the octal number 642? 6 x 82 = 6 x 64 = 384 + 4 x 81 = 4 x 8 = 32 + 2 x 8º = 2 x 1 = 2 = 418 in base 10 CONVERTING HEXADECIMAL TO DECIMAL What is the decimal equivalent of the hexadecimal number DEF? D x 162 = 13 x 256 = 3328 + E x 161 = 14 x 16 = 224 + F x 16º = 15 x 1 = 15 = 3567 in base 10 Remember, the digit symbols in base 16 are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F ARITHMETIC IN BINARY Remember that there are only 2 digit symbols in binary, 0 and 1 1 + 1 is 0 with a carry 1011111 1010111 +1 0 0 1 0 1 1 10100010 Carry Values SUBTRACTING BINARY NUMBERS Remember borrowing? Apply that concept here: 012 02 1010111 - 111011 0011100 CONVERTING BINARY TO OCTAL • Mark groups of three (from right) • Convert each group 10101011 10 101 011 2 5 3 10101011 is 253 in base 8 CONVERTING BINARY TO HEXADECIMAL • Mark groups of four (from right) • Convert each group 10101011 1010 1011 A B 10101011 is AB in base 16 CONVERTING DECIMAL TO OTHER BASES Algorithm for converting number in base 10 to other bases: While the quotient is not zero: Divide the decimal number by the new base Make the remainder the next digit to the left in the answer Replace the original decimal number with the quotient CONVERTING DECIMAL TO BINARY Note that the rightmost digit in a binary number tells us if it is even or odd. Thus, if we divide a number by 2, the remainder tells us the rightmost binary digit. We can convert a number (say, 13410) to binary by dividing by two until we reach zero. The digits in reverse order represent the resulting number: 134 / 2 = 67 rem 0 67 / 2 = 33 rem 1 33 / 2 = 16 rem 1 16 / 2 = 8 rem 0 8/2 =4 rem 0 4/2 =2 rem 0 2/2 =1 rem 0 1/2 =0 rem 1 § The resulting number is thus 100001102. We can check ourselves by converting back to base 10. CONVERTING DECIMAL TO HEXADECIMAL What is 3567 (base 10) in base 16? 222 16 3567 32 36 32 47 32 15 Answer: D EF 13 16 222 16 62 48 14 0 16 13 0 13