Download - Number system
Dec Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
Copyright © 2000, Daniel W. Lewis. All Rights Reserved.
Memorize This!
Hex Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
Hex Binary
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
Copyright © 2000, Daniel W. Lewis. All Rights Reserved.
Changing the Sign
+6 = 0110
-6 = 1110
Sign+Magnitude: 2’s Complement:
+6 = 0110
+4 = 1001
+1
-6 = 1010
Invert
Increment
Change 1 bit
Copyright © 2000, Daniel W. Lewis. All Rights Reserved.
Why 2’s Complement?
+3 0011
+2 0010
+1 0001
0 0000
-1 1111
-2 1110
-3 1101
-4 1100
1. Just as easy to determine sign as in
sign+magnitude.
2. Almost as easy to change the sign of
a number.
3. Addition can proceed w/out
worrying about which operand is
larger.
4. A single zero!
5. One hardware adder works for both
signed and unsigned operands.
Copyright © 2000, Daniel W. Lewis. All Rights Reserved.
Signed Overflow
• Overflow is impossible when adding (subtracting) numbers that have different (same) signs.
• Overflow occurs when the magnitude of the result extends into the sign bit position:
01111111 (0)10000000
This is not rollover!
Subtraction (lvk) 10
Why is it called “one’s complement?”
• Complementing a single bit is equivalent to subtracting it from 1.
0’ = 1, and 1 - 0 = 1 1’ = 0, and 1 - 1 = 0
• Similarly, complementing each bit of an n-bit number is equivalent to subtracting that number from 2n-1.
• For example, we can negate the 5-bit number 01101.
– Here n=5, and 2n-1 = 3110 = 111112.
– Subtracting 01101 from 11111 yields 10010:
1 1 1 1 1- 0 1 1 0 1
1 0 0 1 0
Subtraction (lvk) 11
One’s complement addition
• To add one’s complement numbers:
– First do unsigned addition on the numbers, including the sign bits.
– Then take the carry out and add it to the sum.
• Two examples:
• This is simpler and more uniform than signed magnitude addition.
0111 (+7)+ 1011 + (-4)
1 0010
0010+ 1
0011 (+3)
0011 (+3)+ 0010 + (+2)
0 0101
0101+ 0
0101 (+5)
Subtraction (lvk) 12
Two’s complement
• Our final idea is two’s complement. To negate a number, complement each bit (just as for ones’ complement) and then add 1.
• Examples:
11012 = 1310 (a 4-bit unsigned number)
0 1101 = +1310 (a positive number in 5-bit two’s complement)
1 0010 = -1310 (a negative number in 5-bit ones’ complement)
1 0011 = -1310 (a negative number in 5-bit two’s complement)
01002= 410 (a 4-bit unsigned number)
0 0100 = +410 (a positive number in 5-bit two’s complement)
1 1011 = -410 (a negative number in 5-bit ones’ complement)
1 1100 = -410 (a negative number in 5-bit two’s complement)
Subtraction (lvk) 13
• Two other equivalent ways to negate two’s complement numbers:
– You can subtract an n-bit two’s complement number from 2n.
– You can complement all of the bits to the left of the rightmost 1.
01101 = +1310 (a positive number in two’s complement)
10011 = -1310 (a negative number in two’s complement)
00100 = +410 (a positive number in two’s complement)
11100 = -410 (a negative number in two’s complement)
• Often, people talk about “taking the two’s complement” of a number. This is a confusing phrase, but it usually means to negate some number that’s already in two’s complement format.
More about two’s complement
1 00000- 01 1 01 (+1310)
1 001 1 (-1310)
1 00000- 001 00 (+410)
1 1 1 00 (-410)
Subtraction (lvk) 14
Two’s complement addition
• Negating a two’s complement number takes a bit of work, but addition is much easier than with the other two systems.
• To find A + B, you just have to:
– Do unsigned addition on A and B, including their sign bits.
– Ignore any carry out.
• For example, to find 0111 + 1100, or (+7) + (-4):
– First add 0111 + 1100 as unsigned numbers:
– Discard the carry out (1).
– The answer is 0011 (+3).
01 1 1+ 1 1 001 001 1
Subtraction (lvk) 15
Another two’s complement example
• To further convince you that this works, let’s try adding two negative numbers—1101 + 1110, or (-3) + (-2) in decimal.
• Adding the numbers gives 11011:
• Dropping the carry out (1) leaves us with the answer, 1011 (-5).
1 1 01+ 1 1 1 01 1 01 1
Copyright © 2000, Daniel W. Lewis. All Rights Reserved.
Signed Overflow
-12010 100010002
-1710 +111011112
sum: -13710 1011101112
011101112 (keep 8 bits)
(+11910) wrong
Note: 119 – 28 = 119 – 256 = -137
Rules for binary subtraction are:
0 – 0 = 0
1 – 0 = 1
1 – 1 = 0
0 – 1 = 1 , with 1 borrowed from the next column
19
Rules for binary addition are:0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 with 1 to carry for the next column
1 + 1 + 1 = 1 with 1 to carry for the next column
Binary Addition
Ex 1: Find the sum of the binary numbers 1101 & 110 and verify the result
using decimal numbers
Solution:
(10011)2 = 1*24 + 0*23 + 0*22 + 1*21 + 1*20 = (19)10
20
Ex2: Perform the following binary addition operation then verify the result
using decimal numbers: 110101.101 + 10110.111
Solution:
(1001100.1)2 = 1*26 + 1*23 + 1*22 + 1*2-1
= 64 + 8 + 4 + 0.5 = (76.5)10
21
Rules for binary subtraction are:
0 – 0 = 0
1 – 0 = 1
1 – 1 = 0
0 – 1 = 1 , with 1 borrowed from the next column
Binary Subtraction
Ex 1: Use the direct binary subtraction to get the result of:
1100101 – 100111 Verify the result in decimal system.
Solution:
(10011)2 = 1*24 + 1*21 + 1*20 = (19)10
22
The “complement method” allows performing binary
subtraction in the form of binary addition which is
much easier. This greatly simplifies the design of
the electronic circuits of the digital computers.
Subtraction Using the Complement Method
Examples:
Decimal Subtraction using 9’s and 10’s Complement
Binary Subtraction using 1’s and 2’s Complement
25
Signed Numbers
• Signed-magnitude form – The sign bit is the left-most bit in a signed
binary number
– A 0 sign bit indicates a positive magnitude
– A 1 sign bit indicates a negative
magnitude
26
The “complement method” allows performing binary
subtraction in the form of binary addition which is
much easier. This greatly simplifies the design of
the electronic circuits of the digital computers.
Subtraction Using the Complement Method
Examples:
Decimal Subtraction using 9’s and 10’s Complement
Binary Subtraction using 1’s and 2’s Complement
27
Decimal Subtraction using 9’s and 10’s Complement
Ex 1: Decimal subtraction using 9’s complement
Solution:
The number 6832 is the 9’s complement of 3167
28
Ex 2: Decimal subtraction using 10’s complement
Solution:
The number 6833 is the 9’s complement + 1 of the number
3167. Therefore, it is called the 10’s complement.
The 10’s complement = the 9’s complement + 1
29
Binary Subtraction using 1’s and 2’s Complement
The 1’s complement of a binary number is simply obtained
by replacing every 1 by 0 , and every 0 by 1.
The 2’s complement of a binary number can be obtained in
two ways:
By adding 1 to the 1’s complement.
Start the binary number from right. Leave the binary
digits unchanged until the first 1 appear, then replace
every 1 by 0 , and every 0 by 1.
30
Ex 1: Obtain the two’s complement of the binary number 1011010.110
Second solution
First solution
31
Ex 2: Calculate the following binary Subtraction: 11101.101 – 1011.11 ,
then verify the result in decimal System.
Solution
(10001.111)2 = 16 + 1 + 0.5 + 0.125 = (17. 875)10
32
Important Note:
When using the complement methods in subtraction and
having no additional 1 in the extreme left cell, then , this
means a negative result.
In this case, the solution is the negative of 1’s
complement of the result (if using 1’s complement
initially), or the negative of 2’s complement of the result
(if using 2’s complement initially).
33
Ex 2: Calculate the following binary Subtraction: 1101.101 – 11011.11 ,
then verify the result in decimal System.
Solution
Therefore, the result = - (the 1’s complement of 10001.110) = - 01110.001
Or = - ( the 2’s complement of 10001.111 ) = - 01110.001