1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2
2 2 Positional Notation: Base Quick Re-cap: 1. Conversion from other bases to base 10, where b is the base of the original number, d i is the digit at the i-th position : 2. Conversion from base 10 to other bases EECS Computer Use: Fundamentals d n-1 * b n-1 + … + d 2 * b 2 + d 1 * b 1 + d 0 * b 0 Step 1: Always divide the decimal number by the new base, write down the quotient and the reminder Step 2: Divide the quotient by the new base, write down the new quotient and the new reminder Step 3: Repeat step 2 until the quotient is 0
3 3 Positional Notation: Base In general, we can convert numbers in any bases with the following method: EECS Computer Use: Fundamentals (number) A (number) 10 (number) B where A ≠ B ≠ 10
4 4 Positional Notation: Adding binary numbers Basic idea in adding digits in decimal: = 3, = 5…. EECS Computer Use: Fundamentals When you try to add two numbers whose sum is equal to or greater than the base value (i.e. 10), we reuse the same digits and rely on position. The rules of adding binary numbers are similar, but we run out of digits much faster (since we only have 2 digits) is carried into the next position to the left 1 is called the “carry bit”
5 5 Positional Notation: Adding binary numbers Example 1: what is the sum of the following two binary numbers? EECS Computer Use: Fundamentals We can confirm that the above sum is correct by converting each of the above binary numbers to their decimal representations
6 6 Positional Notation: Adding binary numbers EECS Computer Use: Fundamentals convert to base 10 1* *2 0 = convert to base 10 1* *2 0 = 9 1* *2 2 = 12
7 7 Positional Notation: Adding binary numbers Example 2: what is the sum of the following two binary numbers? EECS Computer Use: Fundamentals convert to base 10 1* * *2 0 = = convert to base 10 1* * *2 0 = = 13 1* *2 2 = 16+4 = 20
8 8 Positional Notation: Power-of-2 number systems EECS Computer Use: Fundamentals Decimal (base 10)Binary (base 2)Octal (base 8)
9 9 Positional Notation: Power-of-2 number systems An unique relationship between binary and octal numbers, as 8 is a power of 2 (i.e. 8 = 2 3 ) Given an octal number, you can convert it directly to binary by replacing each digit in the octal number with the binary representation of that digit (note: the largest digit can only be “7”) EECS Computer Use: Fundamentals DecimalBinaryOctal
10 Positional Notation: Power-of-2 number systems EECS Computer Use: Fundamentals Example: what is the binary representation of the octal “154” ? binary octal
11 Positional Notation: Power-of-2 number systems Now, if we convert the binary number to base 10: EECS Computer Use: Fundamentals We got 108, same as the number we got when we converted the octal number “154” to its base 10 representation: d n-1 * b n-1 + … + d 2 * b 2 + d 1 * b 1 + d 0 * b 0 1 * * * 8 0 = = * * * * 2 2 = = = 108
12 Positional Notation: Power-of-2 number systems Convert from binary number to octal number: EECS Computer Use: Fundamentals Step 1: start at the rightmost binary digit and mark the digits in groups of threes Step 2: convert each group of three into its octal value Note: Add 0s to the front if the leftmost group does not have 3 binary digits Example: what is the octal representation of the binary number: ? Note: any digits > 7 do not exist in octal, if you get an octal value > 7, something is wrong binary octal
13 Positional Notation: Power-of-2 number systems 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Recall that, in base-16, we have: EECS Computer Use: Fundamentals The previous “grouping” method can be applied to convert a binary number to its hexadecimal number (since 16 = 2 4 ) 0000, ……………………………………….., 1111binary hexadecimal
14 Positional Notation: Power-of-2 number systems Convert from binary number to hexadecimal number: EECS Computer Use: Fundamentals Step 1: start at the rightmost binary digit and mark the digits in groups of fours Step 2: convert each group of four into its hexadecimal value Note: Add 0s to the front if the leftmost group does not have 4 binary digits Example: what is the octal representation of the binary number: ? E binary hexadecimal
15 Positional Notation: Power-of-2 number systems Conversion between octal and hexadecimal numbers are much easier now as we can utilize the intermediate binary digits “grouping” method EECS Computer Use: Fundamentals Example: what is the hexadecimal representation of the octal number: 4567 ? octal binary hexadecimal 9 7 7
16 Positional Notation: Adding octal numbers EECS Computer Use: Fundamentals When you try to add two numbers whose sum is equal to or greater than the base value (i.e. 8), we reuse the same digits and rely on position. (7) 8 + (1) 8 (1 0) 8 1 is carried into the next position to the left Example 1: what is the sum of the following two octal numbers? (27) 8 + (35) 8 (64) 8
17 Positional Notation: Adding octal numbers EECS Computer Use: Fundamentals (27) 8 (35) 8 convert to base 10 2* *8 0 = 16+7 = 23 (64) 8 convert to base 10 3* *8 0 = 24+5 = 29 6* *8 0 = 48+4 = 52
18 Positional Notation: Adding hexadecimal numbers EECS Computer Use: Fundamentals Same concept is applied to adding two hexadecimal numbers. When you try to add two hex numbers whose sum is equal to or greater than the base value (i.e. 16), we reuse the same digits and rely on position. (F) 16 + (1) 16 (1 0) 16 1 is carried into the next position to the left (F) 16 + (2) 16 (1 1) 16 (F) 16 + (3) 16 (1 2) 16 (F) 16 + (4) 16 (1 3) 16 and so on…..
19 Positional Notation: Adding hexadecimal numbers EECS Computer Use: Fundamentals Example: (2F) 16 + (73) 16 (A2) 16 (2F) 16 (73) 16 convert to base 10 2* *16 0 = = 47 (A2) 16 convert to base 10 7* *16 0 = = * *16 0 = = 162
20 Positional Notation: Subtracting two base 10 numbers EECS Computer Use: Fundamentals When you try to subtract a larger digit from a smaller one, we have to “borrow one” from the next left digit of the number from which you are subtracting. More precisely, we borrow one power of the base, such as in base 10, we borrow Borrow “10” from 5, so we have = 4 Example:
21 Positional Notation: Subtracting two binary numbers EECS Computer Use: Fundamentals In base 2, when borrow one power of the base to do subtraction, we borrow 2. In fact, we only need to borrow a 2 when we subtract 0 from Example: 0111 (1101) 2 (0110) 2 convert to base (0111) 2 convert to base
22 Positional Notation: Subtracting two octal numbers EECS Computer Use: Fundamentals In base 8, when borrow one power of the base to do subtraction, we borrow 8. (153) 8 - (67) 8 Example: (64) 8 (153) 8 (67) 8 convert to base (64) 8 convert to base
23 Positional Notation: Subtracting two hex numbers EECS Computer Use: Fundamentals In base 16, when borrow one power of the base to do subtraction, we borrow 16. (153) 16 - (67) 16 Example: (EC) 16 (153) 16 (67) 16 convert to base (EC) 16 convert to base