Transcript + (1)

Week 2:
Binary, Octal and Hexadecimal Numbers
•
READING: Chapter 2
1
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Base
Quick Re-cap:
• 1. Conversion from other bases to base 10, where b is the base of the
original number, di is the digit at the i-th position :
dn-1 * bn-1 + … + d2 * b2 + d1 * b1 + d0 * b0
• 2. Conversion from base 10 to other bases
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
2
2
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Base
• In general, we can convert numbers in any bases with the following method:
(number)A
(number)10
(number)B
where A ≠ B ≠ 10
3
3
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Adding binary numbers
• Basic idea in adding digits in decimal: 1 + 2 = 3, 4 + 1 = 5….
• 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.
9
+ 1
1 0
1 is carried into the next
position to the left
• The rules of adding binary numbers are similar, but we run out of digits much
faster (since we only have 2 digits)
+
0
0
0
+
0
1
1
+
1
0
1
1
+ 1
1 0
1 is called the “carry bit”
4
4
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Adding binary numbers
• Example 1: what is the sum of the following two binary numbers?
+
0011
1001
1100
• We can confirm that the above sum is correct by converting each of the
above binary numbers to their decimal representations
5
5
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Adding binary numbers
convert to base 10
1*21 + 1*20 = 3
1001
convert to base 10
1*23 + 1*20 = 9
1100
convert to base 10
1*23 + 1*22 = 12
0011
6
6
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Adding binary numbers
• Example 2: what is the sum of the following two binary numbers?
+
0111
1101
10100
convert to base 10
1*22 + 1*21 + 1*20 = 4+2+1 = 7
1101
convert to base 10
1*23 + 1*22 + 1*20 = 8+4+1 = 13
10100
convert to base 10
1*24 + 1*22 = 16+4 = 20
0111
7
7
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Power-of-2 number systems
Decimal (base 10)
Binary (base 2)
Octal (base 8)
0
0000
0
1
0001
1
2
0010
2
3
0011
3
4
0100
4
5
0101
5
6
0110
6
7
0111
7
8
1000
10
9
1001
11
10
1010
12
8
8
EECS 1520 -- Computer Use: Fundamentals
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 = 23)
• 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”)
Decimal
Binary
Octal
0
000
0
1
001
1
2
010
2
3
011
3
4
100
4
5
101
5
6
110
6
7
111
7
9
9
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Power-of-2 number systems
• Example: what is the binary representation of the octal “154” ?
octal
1
5
4
binary
001
101
100
10
10
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Power-of-2 number systems
• Now, if we convert the binary number to base 10:
001 101 100
1 * 26 + 1 * 25 + 1* 23 + 1 * 22
= 64 + 32 + 8
+ 4 = 64 + 44 = 108
• We got 108, same as the number we got when we converted the octal
number “154” to its base 10 representation:
dn-1 * bn-1 + … + d2 * b2 + d1 * b1 + d0 * b0
1 * 82 + 5 * 81 + 4 * 80
= 64 + 40 + 4 = 108
11
11
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Power-of-2 number systems
• Convert from binary number to octal number:
Step 1:
Step 2:
start at the rightmost binary digit and mark the digits in groups of threes
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: 1101111110 ?
binary
001
octal
1
101 111 110
5
7
6
• Note: any digits > 7 do not exist in octal, if you get an octal value > 7,
something is wrong.
12
12
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Power-of-2 number systems
• Recall that, in base-16, we have:
hexadecimal
binary
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
0000, ……………………………………….., 1111
• The previous “grouping” method can be applied to convert a binary number
to its hexadecimal number (since 16 = 24)
13
13
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Power-of-2 number systems
• Convert from binary number to hexadecimal number:
Step 1:
Step 2:
start at the rightmost binary digit and mark the digits in groups of fours
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: 1101111110 ?
binary
hexadecimal
0011
0111
1110
3
7
E
14
14
EECS 1520 -- Computer Use: Fundamentals
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
• Example: what is the hexadecimal representation of the octal number: 4567 ?
octal
4
5
6
7
binary
100
101
110
111
1001
hexadecimal
9
0111
7
0111
7
15
15
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Adding octal numbers
• 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
16
16
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Adding octal numbers
convert to base 10
2*81 + 7*80 = 16+7 = 23
(35)8
convert to base 10
3*81 + 5*80 = 24+5 = 29
(64)8
convert to base 10
6*81 + 4*80 = 48+4 = 52
(27)8
17
17
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Adding hexadecimal numbers
• 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.
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
+ (1)16
(1 0)16
(F)16
+ (4)16
(1 3)16
and so on…..
18
18
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Adding hexadecimal numbers
• Example:
+
(2F)16
(73)16
(A2)16
convert to base 10
2*161 + 15*160 = 32+15 = 47
(73)16
convert to base 10
7*161 + 3*160 = 112+3 = 115
(A2)16
convert to base 10
10*161 + 2*160 = 160+2 = 162
(2F)16
19
19
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Subtracting two base 10 numbers
• 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 10.
• Example:
-
52
38
14
Borrow “10” from 5, so
we have 10- 8 + 2 = 4
20
20
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Subtracting two binary numbers
• 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 1.
• Example:
-
1101
0110
0111
(1101)2
convert to base 10
13
(0110)2
convert to base 10
6
(0111)2
convert to base 10
7
21
21
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Subtracting two octal numbers
• In base 8, when borrow one power of the base to do subtraction, we borrow
8.
• Example:
-
(153)8
(67)8
(64)8
(153)8
convert to base 10
107
(67)8
convert to base 10
55
(64)8
convert to base 10
52
22
22
EECS 1520 -- Computer Use: Fundamentals
Positional Notation:
Subtracting two hex numbers
• In base 16, when borrow one power of the base to do subtraction, we borrow
16.
• Example:
-
(153)16
(67)16
(EC)16
(153)16
convert to base 10
339
(67)16
convert to base 10
103
(EC)16
convert to base 10
236
23
23