CPS120: Introduction to Computer Science Computer Math: Converting to Decimal Binary Decimal is base 10 and has 10 digits: 0,1,2,3,4,5,6,7,8,9 Binary is base 2 and has 2 digits: 0,1 For a number to exist in a given number system, the number system must include those digits. For example: The number 284 only exists in base 9 and higher. 9 Codes Given any positive integer base (RADIX) N, there are N different individual symbols that can be used to write numbers in the system. The value of these symbols range from 0 to N-1 All systems we use in computing are positional systems 495 = 400 + 90 +5 Number Systems We use the DECIMAL (10 system Computers use BINARY (2 or some shorthand for it like OCTAL (8 or HEXADECIMAL (16 Power of 2 Number System Binary 000 001 Octal 0 1 Decimal 0 1 010 011 100 101 2 3 4 5 2 3 4 5 110 111 6 7 6 7 100 10 8 1001 1010 11 12 9 10 16 Bases Higher than 10 How are digits in bases higher than 10 represented? Base 16: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E, and F 10 Conversions Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 Octal 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 Hex 0 1 2 3 4 5 6 7 8 9 A B C D E Decimal Equivalents Assuming the bits are unsigned, the decimal value represented by the bits of a byte can be calculated as follows: 1. Number the bits beginning on the right using superscripts beginning with 0 and increasing as you move left • 2. 3. 4. Note: 20, by definition is 1 Use each superscript as an exponent of a power of 2 Multiply the value of each bit by its corresponding power of 2 Add the products obtained Converting Octal to Decimal What is the decimal equivalent of the octal number 642? 6 x 8² = 6 x 64 = 384 + 4 x 8¹ = 4 x 8 = 32 + 2 x 8º = 2 x 1 = 2 = 418 in base 10 11 Converting Hexadecimal to Decimal What is the decimal equivalent of the hexadecimal number DEF? D x 16² = 13 x 256 = 3328 + E x 16¹ = 14 x 16 = 224 + F x 16º = 15 x 1 = 15 = 3567 in base 10 Remember, base 16 is 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F Converting Binary to Decimal What is the decimal equivalent of the binary number 010110? 1 x 26 + 1 x 25 + 0 x 24 + 1 x 23 + 1 x 22 + 1 x 21 + 0 x 2º = = = = = = = 1 x 64 = 64 1 x 32 = 32 0 x 16 = 0 1x8 =8 1x4 =4 1x2 =2 0x1 =0 = 112 in base 10 13 Horner’s Method Another procedure to calculate the decimal equivalent of a binary number Note: This method works with any base Horner’s Method: Step 1: Start with the first digit on the left Step 2: Multiply it by the base Step 3: Add the next digit Step 4: Multiply the sum by the base Step 5: Continue the process until you add the last digit