Professional Documents
Culture Documents
Chapter 4
Chapter Overview
© Christian Jacob
Chapter Overview
Page 3 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
2n-1 8 4 2 1
• With 2 bits, each position has 2 possible values: 00, 01, 10, 11
n–1
∑
i n–1 n–2 1 0
Value: N B = di ⋅ B = dn – 1B + dn – 2B + … + d1B + d0B
i=0
First Back TOC Important Number Systems for Computers Prev Next Last
Page 7 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
dual 2 0, 1
binary
octal 8 0, 1, 2, 3, 4, 5, 6, 7
decimal 10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
hexadecimal 16 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
sedecimal
First Back TOC Important Number Systems for Computers Prev Next Last
Page 8 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Z = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ; B = 10
• Each position to the left of a digit increases by a power of 10.
• Each position to the right of a digit decreases by a power of 10.
Example:
4769210 = 2 · 100 +
9 · 101 +
6 · 102 +
7 · 103 +
4 · 104
= 2 + 90 + 600 + 7000 + 40000
First Back TOC Important Number Systems for Computers Prev Next Last
Page 9 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Z = { 0, 1 } ; B = 2
• Each position to the left of a digit increases by a power of 2.
• Each position to the right of a digit decreases by a power of 2.
Example:
101110012 = 1 · 20 +
0 · 21 +
0 · 22 +
1 · 23 +
1 · 24 +
1 · 25 +
0 · 26 +
1 · 27 = 1 + 8 + 16 + 32 + 128 = 18510
First Back TOC Important Number Systems for Computers Prev Next Last
Page 10 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Counting in Binary
First Back TOC Important Number Systems for Computers Prev Next Last
Page 11 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Z = { 0, 1, 2, 3, 4, 5, 6, 7 } ; B = 8
• Each position to the left of a digit increases by a power of 8.
• Each position to the right of a digit decreases by a power of 8.
Example:
124038 = 3 · 80 +
0 · 81 +
4 · 82 +
2 · 83 +
1 · 84
= 3 · 1 + 0 · 8 + 4 · 64 + 2 · 512 + 1 · 4096
= 537910
First Back TOC Important Number Systems for Computers Prev Next Last
Page 12 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Counting in Octal
First Back TOC Important Number Systems for Computers Prev Next Last
Page 13 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Z = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F } ; B = 16
Example:
FB40A16 = 10 · 160 +
0 · 161 +
4 · 162 +
11 · 163 +
15 · 164
= 10 · 1 + 0 · 16 + 4 · 256 + 11 · 4096 + 15 · 65536
= 1,029,13010
First Back TOC Important Number Systems for Computers Prev Next Last
Page 14 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Counting in Hexadecimal
First Back TOC Important Number Systems for Computers Prev Next Last
Page 15 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
First Back TOC Important Number Systems for Computers Prev Next Last
Page 16 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
4.3 Powers of 2
N 2N N 2N
0 1 17 131,072
1 2 18 262,144
2 4 19 524,288
3 8 20 1,048,576
4 16 21 2,097,152
5 32 22 4,194,304
6 64 23 8,388,608
7 128 24 16,777,216
8 256 25 33,554,432
9 512 26 67,108,864
10 1,024 27 134,217,728
11 2,048 28 268,435,456
12 4,096 29 536,870,912
13 8,192 30 1,073,741,824
14 16,384 31 2,147,483,648
15 32,768 32 4,294,967,296
16 65,536 33 8,589,934,592
First Back TOC Conversions Between Number Systems Prev Next Last
Page 18 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Example: 102010 = ? 8
Step 4: 1 : 8 = 0 Remainder: 1
⇒ 102010 = 17748
First Back TOC Conversions Between Number Systems Prev Next Last
Page 19 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
1F216 = 2 · 160 +
15 · 161 +
1 · 162
= 2 + 240 + 256
⇒ 1F216 = 49810
First Back TOC Conversions Between Number Systems Prev Next Last
Page 20 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
• Conversion 2n ⇒ 21
n = 3: octal ⇒ dual:
Replace 1 octal digit by 3 binary digits.
n = 4: hexadecimal ⇒ dual
Replace 1 hexadecimal digit by 4 binary digits.
First Back TOC Conversions Between Number Systems Prev Next Last
Page 21 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
• Conversion 21 ⇒ 2n
n = 3: dual ⇒ octal:
Replace 3 binary digits by 1 octal digit.
= 5 7 28
n = 4: hexadecimal ⇒ dual
Replace 4 binary digits by 1 hexadecimal digit.
= 3 1 F 216
First Back TOC Conversions Between Number Systems Prev Next Last
Page 22 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
• Conversion 2n ⇒ 2m
2n ⇒ 21 ⇒ 2m
Example: octal ⇒ hexadecimal
263518 = 2 6 3 5 18
= 2 C E 916
First Back TOC Conversions Between Number Systems Prev Next Last
Page 23 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
First Back TOC Conversions Between Number Systems Prev Next Last
Page 24 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Step i N Operation R z( –i )
First Back TOC Conversions Between Number Systems Prev Next Last
Page 25 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Logical AND ( x ∧ y )
AND 0 1
0 0 0
1 0 1
Logical OR ( x ∨ y )
OR 0 1
0 0 1
1 1 1
x ¬x
0 1
1 0
XOR 0 1
0 0 1
1 1 0
NAND 0 1
0 1 1
1 1 0
Examples:
Examples:
Addition: Subtraction:
1010 1101
+ 1011 - 1010
––––––––––––––––- ––––––––––––––––-
carry: 1 0 1 1 carry: 1 0 1 0
+ 10111 -1 110
––––––––––––––––- ––––––––––––––––-
10101 0011
1010 1310
+ 1110 - 1010
––––––––––––––––- ––––––––––––––––-
2110 310
Multiplication:
111·101
––––––––––––––––––––––––
1 1 1
00 0
+ 1 1 1
––––––––––––––––––––––––
carry: 1
+ 1
carry: 1
+ 1
carry: 1
+ 1
––––––––––––––––––––––––
1 0 0 0 1 1
15 14 1 0
sign value
First Back TOC Negative Numbers and Complements Prev Next Last
Page 32 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Example (1): n = 3
1 01 -1
1 10 -2
1 11 -3
First Back TOC Negative Numbers and Complements Prev Next Last
Page 33 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
• “1-1”: 1 - 1 = 1 + (-1) = 0 ?
0 001 001
+ 1 001 - 001
–––––––––––– –––––––––
1 0 1 0 (= -210) 000
First Back TOC Negative Numbers and Complements Prev Next Last
Page 34 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Example:
Problem!
First Back TOC Negative Numbers and Complements Prev Next Last
Page 35 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Examples:
First Back TOC Negative Numbers and Complements Prev Next Last
Page 36 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
01111
10001
01 10
-16
100 1
01 101
10
011
Addition Subtraction
10
01
0
10
10
10 -14-15 15 14
10
-13
10 -12 18 17 13 1 1
0
0
101 1 -11 20 19 12 1
11 0 1 0
10 1 0
-10 21 10 0
10111 01
9 010
22
-9 23
11000 -8 24 negative positive 8 01000
numbers numbers
1 -7 25 7 001
1 1 0 0 11
-6 26 6
10 001
1 1 0 27
-5 28 50 10
11 -4 2930 4 01
0 01
11 -3 31 3
00 011
10 0
-2 2
10
-1 1
10
00 10
111 1
11
000
10
0
00001
11111
11
00000
First Back TOC Negative Numbers and Complements Prev Next Last
Page 37 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Example 1 Example 2
B = 10 1410 - 710 910 - 1310
Complement 7: 00111 13: 01101
(-7)B-1: 11000 (-13)B-1: 10010
+ 1 + 1
––––––– –––––––
(-7)B: 11001 (-13)B: 10011
Addition 14: 01110 9: 01001
+(-7)B: + 11001 +(-13)B: + 10011
––––––––– –––––––––
7: 1 00111 -4: 11100
First Back TOC Negative Numbers and Complements Prev Next Last
Page 38 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob
Hence, the usual format for a 32-bit real (floating point) number is:
Addition:
• 1. Identify mantissae and exponents
• 2. Compare exponents
• 3. Adjust exponents if necessary
• 4. Adjust the shorter mantissa
• 5. Add mantissae
• 6. Normalize mantissa
• 7. Adjust exponent
Example: Multiplication
• 1. Multiply of mantissae
• 2. Normalize mantissae
• 3. Adjust exponents
• 4. Add exponents
0.792 · 10-1
+ 0.792 · 10-2
+ 4.752 · 10-3
0.871 · 10-1
0.918 · 10-1
• Result:
Example:
- mantissa: 2 decimal digits
- exponent: 1 decimal digit
• Sample number: 74 · 102 = 7400
What is the next higher value?
75 · 102 = 7500
What about values 7400 < x < 7500?
⇒ They cannot be represented!!!
1 2 3 4 5 6 7 8 n-1 n
5 P e t e r