Professional Documents
Culture Documents
Chapter 2
Chapter 2
Where:
N: an integer with n+1 digits
r: base
ai {0, 1, 2, … , r-1}
N = 26316 => n = 2; a2 = 2; a1 = 6; a0
= 3
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 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F
16 10000 20 10
b)000110102 (8 bits)
= 24 + 23 +21
= 16 + 8 + 2
= 2610
0 5 7 3 6 4
= 578 = 3648
SSK3207 - Chapter 2 SSK3207-Chapter 2 15
Binary and Hexadecimal
• The same method employed in binary-octal
conversion is used once again.
• Assume that:
R1 = 16 (hexadecimal)
R2 = 2 (binary)
• From the theorem: 16 = 24
Hence, 4 digits in a binary number is
equivalent to 1 digit in the hexadecimal
number system (and otherwise)
• The following is the binary-hexadecimal
conversion table
68 58 18 28 38
110 101 001 010 011
octal binary hexadecimal
0000 0000 0011 01012 0000 0000 0101 00112
0 0 3 5 0 0 5 3
= 3516 = 5316
SSK3207 - Chapter 2 SSK3207-Chapter 2 18
Example: Hexadecimal Binary
1 2 B16 A B C D16
0001 0010 10112 (12 bits) 1010 1011 1101 11102
= 0001001010112 = 10101011110111102
SSK3207 - Chapter 2 SSK3207-Chapter 2 19
Exercise
Binary decimal
001100
11100.011
Decimal binary
145
34.75
410.35
Octal hexadecimal
56558
11100.011 = 28.375
Decimal binary
145/2 = 72 (reminder 1); 72/2=36(r=0); 36/2=18(r=0);
145 = 10010001 18/2=9(r=0); 9/2=4(r=1); 4/2=2(r=0); 2/2=1(r=0); ½=0(r=1)
34.75 = 100010.11
Octal hexadecimal
octal binary decimal hexadecimal
56558 = BAD Octal binary
101:110:101:101
Binary hexadecimal
1011:1010:1101
B A D
a) +7 = 0 0 0 0 0 1 1 1
(–7 = 100001112)
b) –10 = 1 0 0 0 1 0 1 0
(+10 = 000010102)
SSK3207 - Chapter 2 SSK3207-Chapter 2 25
ii) 6 bits binary number
__ __ __ __ __ __
a) +7 = 0 0 0 1 1 1
(–7 = 1 0 0 1 1 12)
b) –10 = 1 0 1 0 1 0
(+10 = 0 0 1 0 1 02)
Solution: Solution:
(-10) 10 = 1101012 + 12
= 1101102
So, twos compliment for –10
SSK3207 - Chapter 2 is 110110
SSK3207-Chapter 2 2 32
c) Character Representation
Example:
i. 0101112 + 0111102 = 1101012
ii. 1000112 + 0111002 = 1111112
Exercise:
i. 1000100 – 010010 v. 110111 + 001101
ii. 1010100 + 1100 vi. 111000 + 1100110
iii. 110100 – 1001 vii. 110100 x 10
iv. 11001 x 11 viii. 11001 - 1110
11110111
+ 1
11110002 = –710
the answer
the answer
Note:
For cases other than 1 & 2 above, overflow does not occur
and there will be no EAC and the need to perform addition to
the rightmost bit does not arise
Summary
Overview
• Numbers
– Decimal, Binary, Octal, Hexadecimal
– Their relationship
• Sign-magnitude
• One’s complement
• Two’s complement
• Arithmetic operation
Positive Numbers
• A computer represents positive integers in
binary
• Three methods for representing negative
numbers (signed numbers) are
– Sign-magnitude
– One’s complement
– Two’s complement
Sign Magnitude
• Representation the number’s sign and
magnitude (value)
• Positive numbers 0 and negative
numbers 1
• e.g. 01000000 = 64 and 11000000 = -64
• Easy for human to understand but it
requires some special logic for arithmetic
operations (addition, subtraction)
Arithmetic Operation
• Computers mostly storing information ,
particularly integers, in byte form
• In this section we look how addition and
subtraction are performed in computers
• To perform mathematical functions, a CPU
contains a component called ALU (arithmetic
logic unit) – to perform arithmetic operations
(addition & subtraction) and logic operation (in
next chapter)
One’s Complement
• The negative number is represented by flipping
the number’s bits
• E.g., 01001001 becomes 10110110
• E.g. consider 103 + -97
01100111 + (-01100001) = 01100111 + 10011110
= 100000101 (9 bits)
00000101 + 1 = 00000110 (8 bits)
103 – 97 = 6 = 00000110
Try 113 + -42 = ?? and –75 + 13 = ??
Two’s Complement
• Makes addition and subtraction simple
• The fact that all numbers are stored in 2’s
complement form allows the ALU to use
just one circuit for all basic mathematical
operations
• Visual Basic uses 2’s complement
representations of numbers
Two’s Complement
• Rules
– Just add all the bits
– Throw away EAC (“end around carry”)
– if a – b becomes a + (-b)
– e.g.
111111 (-1) 10110 (-10)
+ 001000 (8) + 11101 (-3)
1000111 (7) 110011 (-13)
• try again –75 + 13 in 2’s complement
Exercise 1
• Question 1.9
(i) -117 – 145 (ii) –90 – 133
- 117 = 10001011 (2’s) -90 = 10100110 (2’s)
- 145 = 01101111 (2’s) -133 = 01111011 (2’s)
Answer : 11111010 Answer: 100100001
Verify: Verify:
00000110 ≠ - 262 11011111 = -223