Signed-Numbers. Two’s Complement...
Transcript of Signed-Numbers. Two’s Complement...
-
Signed-Numbers. Two’s Complement Arithmetic.
(Class 1.2 – 1/17/2013)
CSE 2441 – Introduction to Digital Logic
Spring 2013
Instructor – Bill Carroll, Professor of CSE
-
Today’s Topics
• Signed number representation
• Two’s complement number systems
• Two’s complement arithmetic
-
Signed-Number Representation (1)
• Typical formats for integers and fractions
• Signed-number representation • Sign-magnitude • 2’s complement • 1’s complement
-
Signed Number Representation (2)
• Signed Magnitude Method – N = (an-1 ... a0.a-1 ... a-m)r is represented as N = (san-1 ... a0.a-1 ... a-m)rsm, (1.6) where s = 0 if N is positive and s = r -1 otherwise. – N = -(15)10 – In binary: N = -(15)10 = -(1111)2 = (1, 1111)2sm – In decimal: N = -(15)10 = (9, 15)10sm
• Complementary Number Systems – Radix complements (r's complements, 2’s complement for r=2) [N]r = r
n - (N)r (1.7) where n is the number of digits in (N)r. – Positive full scale: rn-1 - 1 – Negative full scale: -rn - 1 – Diminished radix complements (r-1’s complements, 1’s complement for r=2)
[N]r-1 = rn - (N)r - 1
-
Signed Binary Numbers (1)
Sign-Magnitude Method – N = (an-1 ... a0)2 is represented as N = (san-1 ... a0.a-1 ... a-m)2sm, where s = 0 if N is positive
and s = 1 if N is negative. – If N = +(1011)2, then N = (01011)2sm – If N = -(1011)2, then N = (11011)2sm
Pros and cons – Simple and easy for humans understand – Complicates computer arithmetic units
• Two representations of zero, +0 and -0 • Need both an adder and a subtractor
-
Signed Binary Numbers (2)
Two’s Complement Method – Let N = (an-1 ... a0)2 – If N ≥ 0, it is represented by (0an-1 ... a0)2 – If N < 0, it is represented by [0an-1 ... A0]2 – 2’s complement [N]2 = 2
n - (N)2 , where n is the number of digits in (N)2. – Examples (n = 5, 2n = 100000)
• +(13)10 = +(1101)2 = (01101)2 • -(13)10 = -(1101)2 = [01101]2 = (100000-01101)2 = (10011)2 • +(6)10 = +(0110)2 = (00110)2 • -(6)10 = -(0110)2 = [00110]2 = (100000-00110)2 = (11010)2 • (13)10 – (6)10 = (13)10 + (-(6)10) = (01101)2 + [00110]2 = (01101)2 + (11010)2 =
(100111)2 = +(7)10 • (13)10 – (13)10 = (01101)2 + [01101]2 = (01101)2 + (10011)2 = (100000)2 = 0
– Note that 2’s complement is equivalent to bit-by-bit complement + 1.
-
Signed Binary Numbers (3)
One’s Complement Method – Let N = (an-1 ... a0)2 – If N ≥ 0, it is represented by (0an-1 ... a0)2 – If N < 0, it is represented by [0an-1 ... A0]1 – 1’s complement [N]1 = 2
n - (N)2 – 1 = [N]2 -1 – Examples (n = 5, 25 = 100000)
• +(13)10 = +(1101)2 = (01101)2 • -(13)10 = -(1101)2 = [01101]1 = (100000-01101-1)2 = (10010)2 • +(6)10 = +(0110)2 = (00110)2 • -(6)10 = -(0110)2 = [00110]1 = (100000-00110-1)2 = (11001)2 • (13)10 – (6)10 = (13)10 + (-(6)10) = (01101)2 + [00110]1 = (01101)2 + (11001)2
= (100110)2 = +(6)10 ??? Must correct using end-around carry. • (13)10 – (13)10 = (01101)2 + [01101]1 = (01101)2 + (10010)2 = (11111)2 = -0
– Note that 1’s complement is equivalent to bit-by-bit complement.
-
Signed Binary Numbers (4) Decimal Sign-Magnitude Two’s Complement One’s Complement
+7 0111 0111 0111
+6 0110 0110 0110
+5 0101 0101 0101
+4 0100 0100 0100
+3 0011 0011 0011
+2 0010 0010 0010
+1 0001 0001 0001
0 0000, 1000 0000 0000, 1111
-1 1001 1111 1110
-2 1010 1110 1101
-3 1011 1101 1100
-4 1100 1100 1011
-5 1101 1011 1010
-6 1110 1010 1001
-7 1111 1001 1000
-8 NA 1000 NA
-
Two’s Complement Algorithms (1)
• Algorithm 1.4 Find [N]2 given (N)2 . – Copy the bits of N, beginning with the LSD and proceeding
toward the MSD until the first 1 bit is reached. – Leave this 1 as is. – Replace each remaining digit aj , of N by 1 - aj until the MSD has
been replaced,i.e., replace 1 with 0 and 0 with 1.
• Example: 2's complement of (10100)2 is (01100)2. • Example: 2’s complement of N = (10110)2 for n = 8.
– Put three zeros in the MSB position and apply algorithm 1.4 – N = 00010110 – [N]2 = (11101010)2
-
Two’s Complement Algorithms (2)
• Algorithm 1.5 Find [N]2 given (N)2.
First replace each digit, ak , of (N)r by 1 - ak and then add 1 to the resultant.
• This algorithm follows from the equation [N]1 = 2n - (N)2 – 1 = [N]2 -1, i.e.,
[N]2 = [N]1 + 1
• This is equivalent to complementing each digit and then adding 1.
• Example: Find the 2’s complement of N = (10100)2 .
– N = 10100
01011 complement the bits
+1 add 1
[N]2 = 01100
• Example: Find 2’s complement of N = (01100101)2 .
N = 01100101
10011010 Complement the bits
+1 Add 1
[N]2 = (10011011)2
-
Two’s Complement Arithmetic (1)
• Two’s complement number systems are used in computer systems since this reduces hardware requirements (only adders are needed).
• A - B = A + (-B) (add r’s complement of B to A) • Range of numbers in two’s complement number system, where n is the number of
bits.
• 2n-1 -1 = (0, 11 ... 1)2cns and -2n-1 = (1, 00 ... 0)2cns
• If the result of an operation falls outside the range, an overflow condition is said to
occur and the result is not valid. • Consider three cases (where B 0 and C 0):
– A = B + C – A = B - C – A = - B - C
-
Two’s Complement Arithmetic (2)
• Case 1: A = B + C
– (A)2 = (B)2 + (C)2
– If A > 2n-1 -1 (overflow), it is detected by the nth bit, which is set to 1.
– Example: (7)10 + (4)10 = ? using 5-bit two’s complement arithmetic.
• + (7)10 = +(0111)2 = (0, 0111)2cns
• + (4)10 = +(0100)2 = (0, 0100)2cns
• (0, 0111)2cns + (0, 0100)2cns = (0, 1011)2cns = +(1011)2 = +(11)10
• No overflow.
– Example: (9)10 + (8)10 = ?
• + (9)10 = +(1001)2 = (0, 1001)2cns
• + (8)10 = +(1000)2 = (0, 1000)2cns
• (0, 1001)2cns + (0, 1000)2cns = (1, 0001)2cns (overflow)
-
Two’s Complement Arithmetic (3)
• Case 2: A = B - C – A = (B)2 + (-(C)2) = (B)2 + [C]2 = (B)2 + 2
n - (C)2 = 2n + (B - C)2
– If B C, then A 2n and the carry is discarded. – So, (A)2 = (B)2 + [C]|carry discarded – If B < C, then A = 2n - (C - B)2 = [C - B]2 or A = -(C - B)2 (no carry in
this case). – No overflow for Case 2. – Example: (14)10 - (9)10 = ?
• Perform (14)10 + (-(9)10) • (14)10 = +(1110)2 = (0, 1110)2cns • -(9)10 = -(1001)2 = (1, 0111)2cns • (14)10 - (9)10 = (0, 1110)2cns + (1, 0111)2cns = (0, 0101)2cns + carry
= +(0101)2 = +(5)10
-
Two’s Complement Arithmetic (4)
– Example: (9)10 - (14)10 = ? • Perform (9)10 + (-(14)10) • (9)10 = +(1001)2 = (0, 1001)2cns • -(14)10 = -(1110)2 = (1, 0010)2cns • (9)10 - (14)10 = (0, 1001)2cns + (1, 0010)2cns = (1, 1011)2cns
= -(0101)2 = -(5)10
– Example: (0, 0100)2cns - (1, 0110)2cns = ?
• Perform (0, 0100)2cns + (- (1, 0110)2cns) • - (1, 0110)2cns = two’s complement of (1,0110)2cns = (0, 1010)2cns • (0, 0100)2cns - (1, 0110)2cns = (0, 0100)2cns + (0, 1010)2cns = (0, 1110)2cns = +(1110)2 = +(14)10 • +(4)10 - (-(10)10) = +(14)10
-
Two’s Complement Arithmetic (5)
• Case 3: A = -B - C – A = [B]2 + [C]2 = 2
n - (B)2 + 2n - (C)2 = 2
n + 2n - (B + C)2 = 2n + [B + C]2
– The carry bit (2n) is discarded. – An overflow can occur, in which case the sign bit is 0. – Example: -(7)10 - (8)10 = ?
• Perform (-(7)10) + (-(8)10) • -(7)10 = -(0111)2 = (1, 1001)2cns , -(8)10 = -(1000)2 = (1, 1000)2cns • -(7)10 - (8)10 = (1, 1001)2cns + (1, 1000)2cns = (1, 0001)2cns + carry
= -(1111)2 = -(15)10
– Example: -(12)10 - (5)10 = ? • Perform (-(12)10) + (-(5)10) • -(12)10 = -(1100)2 = (1, 0100)2cns , -(5)10 = -(0101)2 = (1, 1011)2cns • -(7)10 - (8)10 = (1, 0100)2cns + (1, 1011)2cns = (0, 1111)2cns + carry • Overflow, because the sign bit is 0.
-
Two’s Complement Arithmetic (6)
Example: A = (25)10 and B = -(46)10
• A = +(25)10 = (0, 0011001)2cns , -A = (1, 1100111)2cns • B = -(46)10 = -(0, 0101110)2 = (1, 1010010)2cns , -B = (0, 0101110)2cns
• A + B = (0, 0011001)2cns + (1, 1010010)2cns = (1, 1101011)2cns = -(21)10 • A - B = A + (-B) = (0, 0011001)2cns + (0, 0101110)2cns • = (0, 1000111)2cns = +(71)10 • B - A = B + (-A) = (1, 1010010)2cns + (1, 1100111)2cns • = (1, 0111001)2cns + carry = -(0, 1000111)2cns = -(71)10 • -A - B = (-A) + (-B) = (1, 1100111)2cns + (0, 0101110)2cns • = (0, 0010101)2cns + carry = +(21)10 • Note: Carry bit is discarded.
-
Two’s Complement Arithmetic (7)
• Summary • When numbers are represented using 2’s complement number system:
– Addition: Add two numbers. – Subtraction: Add two’s complement of the subtrahend to the minuend. – Carry bit is discarded, and overflow is detected as shown above.
Case Carry Sign Bit Condition Overflow ?
B + C 0
0
0
1
B + C 2n-1 - 1
B + C > 2n-1 - 1
No
Yes
B - C 1
0
0
1
B C
B > C
No
No
-B - C 1
1
1
0
-(B + C) -2n-1
-(B + C) < -2n-1No
Yes