Number System
Number System
n Number Systems
n Decimal
n Binary
n Hexadecimal
n Octal
n Addition
n Subtraction
n 2’s Complement
n Multiplication
n Division
Number System
n A system to express numbers
n Decimal: Base 10:
n 0,1,2,3,4,5,6,7,8,9
n Binary: Base 2:
n 0,1
n Hexadecimal: Base 16:
n 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
n Octal: Base 8:
n 0,1,2,3,4,5,6,7
Number Systems
Motivation
n Where might you encounter number systems in
other courses:
n EECS 2031
EECS 2031/2032
EECS 3214: Networks
Number System
Decimal:
n Base 10
n Value of 5318:
n Xn Xn-1…..X0 = (Xn * Basen) + (Xn-1 * Basen-1) + ….. + (X0* Base0)
1000 100 10 1
5 3 1 8
16 8 4 2 1
1 1 0 1 0
n Move from left to right, Put 1 if the column number fits into the
decimal number. Put 0 if it does not
n Then subtract the decimal number from the column number. Repeat
for the new number until reaching 1
n 84 – 64 = 20 remans to allocate
64 32 16 8 4 2 1
1 0 1
n 20 – 16 = 4 remans to allocate
64 32 16 8 4 2 1
1 0 1
n 4 – 4 = 0 Done
64 32 16 8 4 2 1
1 0 1 0 1 0 0
Decimal To Binary Conversion
Hexadecimal
n Base 16
n Value of ABC3
n Xn Xn-1…..X0 = (Xn * Basen) + (Xn-1 * Basen-1) + ….. + (X0* Base0)
A B C 3
n 7401 – 4096 = 3305 Remains. 256 fits into 3305 Twelve Times:
4096 256 16 1
1 C
n 3305 – (12 * 256) = 233 Remains. 16 fits into 233 Fourteen Time
4096 256 16 1
1 C E
Decimal Addition
Binary Addition
Binary Addition
Binary Addition
• Add: 11010 + 11100
1 1
1 1 0 1 0
1 1 1 0 0 +
1 1 0 1 1 0
Binary Addition
Hex Addition
n Add: AB4 + DE6
1 1
A B 4
D E 6 +
1 8 9 A
Hex Addition
Subtraction
n Subtract: 64 -16
5 14
6 4
1 6 -
4 8
Decimal Subtraction
Binary Subtraction
0 1 1 1 0
Binary Subtraction
Binary Subtraction
• Subtract: 1001 - 110
1
0 10 10
1 0 0 1
1 1 0 -
0 0 1 1
Binary Subtraction
Complements (Decimal)
n 9s Complement: Subtract 9 by each digit
n 15:
9 9
1 5 -
8 4
10s Complement = 9s Complement + 1
15: 84 + 1 = 85
10s Complement
Complements (Binary)
n 1s Complement: Subtract 1 by each digit
n 10110: 1 1 1 1 1
1 0 1 1 0 -
0 1 0 0 1
n 2s Complement = 1s Complement + 1
0 1 0 0 1
0 0 0 0 1 +
1 0 1 0 1 0
Negative sign Bit
Subtracting With Complement
n X1 - X2: (Decimal)
n Get 10s Complement of X2
n 10s Complement = 9s Complement + 1
n Add X1 and 10s Complement of X2
n If the sum has a carry out:
n Sum is a positive number à Discard the carry
n If the sum has no carry out:
n Sum is a negative number à Get 10s
Complement of the sum
Subtracting with Complements
Subtraction Using Complement
n Subtract: 64 -16:
n 1) 9s Complement of 16:
n 99 – 16 = 83
n 2) 10s Complement of 16:
n 83 + 1 = 84
n 3) 64 + 84:
1 6 4
8 4
4 8
Subtraction Using Complement
n Subtract: 16 - 64:
n 1) 9s Complement of 64:
n 99 – 64 = 35
n 2) 10s Complement of 64:
n 35 + 1 = 36
n 3) 16 + 36: 1 6
3 6 +
10s Complement
(No Carry) 5 2 à 48
n Ans: - 48 (Add minus sign because the number is
negative
Subtracting With Complement
n X1 - X2: (Binary)
n Get 2s Complement of X2
n 2s Complement = 1s Complement + 1
n Add X1 and 2s Complement of X2
n If the sum has a carry out:
n Sum is a positive number à Discard the carry
n If the sum has no carry out:
n Sum is a negative number à Get 2s
Complement of the sum
Subtracting with Complements
Subtracting With Complement
n Subtract: 11010 – 1100 (26-12)
n 1) 1s Complement of 1100
n 0011 (Invert all the bits : 0 -> 1, 1 -> 0)
n We need to add a 1 in the MSB (Negative bit): 10011
n 2) 2s Complement of 1100
n 10011 + 1 = 10100
n 3) Add 11010 + 10100 (Discard the carry)
1 1 0 1 0
1 0 1 0 0
1 0 1 1 1 0 = +14
Subtracting with Complements
Subtracting With Complement
n Subtract: 1100 – 11010 (12 - 26)
n 1) 1s Complement of 11010
n 00101 (Invert all the bits : 0 -> 1, 1 -> 0)
n We need to add a 1 in the MSB: 00101 -> 100101
n 2) 2s Complement of 11010 = 100101 + 1 = 100110
n 3) Add 001100 + 100110
0 0 1 1 0 0
1 0 0 1 1 0 +
1 1 0 0 1 0
No Carry: Negative number, we take 2s Complement and add negative
sign à 001110 Ans: -1110 = -14
Multiplication in Decimal
n Multiply: 23 * 15
1
2 3
1 5 x
1 1 5
2 3 0 +
3 4 5
Decimal Multiplication
Multiplication in Binary
n Multiply: 11001 x 101 (25 x 5)
1 1 0 0 1
1 0 1 x
1 1 0 0 1
0 0 0 0 0 0
1 1 0 0 1 0 0
1 1 1 1 1 0 1 = 125
Binary Multiplication
Division in Binary
n Divide: 110101/101 (53 / 5)
1 010
101 110101
101
00110
101
001 1
Division in Binary
n Divide: 101101/110 (45 / 6)
0111
110 101101
110
1010
110
100 1
110
011