Mod 13

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

Addition and Subtraction – 2’s Complement

4 0100 -4 1100
+3 0011 + (-3) 1101
If carry-in to the high
order bit =
carry-out then ignore
carry

if carry-in differs from 4 0100 -4 1100


carry-out then overflow
-3 1101 +3 0011

Simpler addition scheme makes twos complement the most common


choice for integer number systems within digital systems
The rules for addition and subtraction of n-bit signed
numbers using the 2’s-complement representation

 To add two numbers, add their n-bit representations, ignoring the


carry-out bit from the most significant bit (MSB) position. The sum
will be the algebraically correct value in2’s-complement
representation if the actual result is in the range −2 n−1 through
+2 n−1− 1.
 To subtract two numbers X and Y , that is, to perform X − Y , form

the 2’s-complementof Y , then add it to X using the add rule.


Again, the result will be the algebraically correct value in 2’s-
complement representation if the actual result is in the range
−2 n−1through +2 n−1− 1
2’s-Complement Add and Subtract
Operations
(a) 0010 ( + 2) (b) 0100 ( + 4)
+ 0011 ( + 3) + 1010 (- 6)
Page 31 (- 2)
0101 ( + 5) 1110
(c) 1011 (- 5) (d) 0111 ( + 7)
+ 1110 (- 2) + 1101 ( - 3)
1001 (- 7) 0100 ( + 4)
(e) 1101 (- 3) 1101
- 1001 (- 7) + 0111
0100 ( + 4)
(f) 0010 ( + 2) 0010
- 0100 ( + 4) + 1100
1110 ( - 2)
(g) 0110 ( + 6) 0110
- 0011 ( + 3) + 1101
0011 ( + 3)
(h) 1001 ( - 7) 1001
- 1011 (- 5) + 0101
1110 ( - 2)
(i) 1001 (- 7) 1001
- 0001 ( + 1) + 1111
1000 ( - 8)
(j) 0010 ( + 2) 0010
- 1101 ( - 3) + 0011
0101 ( + 5)

Figure 2.4. 2's-complement Add and Subtract operations.


Overflow condition

 Ifcarry-in to the high order bit (MSB) = carry-out of higher


order bit (MSB)
 then no overflow ignore the carry

 Ifcarry-in to the high order bit (MSB) != carry-out of higher


order bit (MSB)
 then overflow condition , result is erroneous
Overflow - Add two positive numbers to get a negative number
or two negative numbers to get a positive number

-1 +0 -1 +0
-2 1111 0000 +1 -2 1111 0000 +1
1110 0001 1110 0001
-3 +2 -3
1101 1101 +2
0010 0010
-4 -4
1100 0011 +3 1100 0011 +3
-5 1011 -5 1011
0100 +4 0100 +4
1010 1010
-6 0101 -6 0101
1001
+5 +5
0110 1001
0110
-7 1000 0111 +6 -7 1000 +6
0111
-8 +7 -8 +7

5 + 3 = -8 -7 - 2 = +7
Overflow Conditions
1000
5 0101 -7 1001
3 0011 -2 1100

Overflow Overflow
0000 1111
5 0101 -3 1101
2 0010 -5 1011

No overflow No overflow
Overflow when carry-in to the high-order bit does not equal carry out
Sign Extension
 Task:
 Given w-bit signed integer x
 Convert it to w+k-bit integer with same value

 Rule:
 Make k copies of sign bit:

 X= x
w–1 ,…, xw–1 , xw–1 , xw–2 ,…, x0
w
X • • •
k copies of MSB

• • •

X • • • • • •
k w
Sign Extension Example

short int x = 15213;


int ix = (int) x;
short int y = -15213;
int iy = (int) y;

Decimal Hex Binary


x 15213 3B 6D 00111011 01101101
ix 15213 00 00 C4 92 00000000 00000000 00111011 01101101
y -15213 C4 93 11000100 10010011
iy -15213 FF FF C4 93 11111111 11111111 11000100 10010011

You might also like