Integer Data Representation
Integer Data Representation
Integer Data Representation
Signed Integer
• 3 major representations:
Sign-magnitude
One’s complement
Two’s complement
• Assumptions:
4-bit machine word
16 different values can be represented
Roughly half are positive, half are negative
Sign and Magnitude Representation
-7 +0
-6 1111 0000 +1
1110 0001
-5 +2 +
1101 0010
-4 1100 0011 +3 0 100 = + 4
-0 +0
-1 1111 0000 +1
1110 0001
-2 +2 +
1101 0010
-3 1100 0011 +3 0 100 = + 4
-1 +0
-2 1111 0000 +1
1110 0001
-3 +2 +
like 1's comp 1101 0010
except shifted -4 1100 0011 +3 0 100 = + 4
one position
clockwise -5 1011 1 100 = - 4
0100 +4
1010 0101
-6 +5 -
1001 0110
-7 1000 0111 +6
-8 +7
Sign and
b3 b2b1b0 magnitude 1's complement 2's complement
0 1 1 1 +7 +7 +7
0 1 1 0 +6 +6 +6
0 1 0 1 +5 +5 +5
0 1 0 0 +4 +4 +4
0 0 1 1 +3 +3 +3
0 0 1 0 +2 +2 +2
0 0 0 1 +1 +1 +1
0 0 0 0 +0 +0 +0
1 0 0 0 -0 -7 -8
1 0 0 1 -1 -6 -7
1 0 1 0 -2 -5 -6
1 0 1 1 -3 -4 -5
1 1 0 0 -4 -3 -4
1 1 0 1 -5 -2 -3
1 1 1 0 -6 -1 -2
1 1 1 1 -7 -0 -1
1. Octal
2. Hexa-Decimal
Conversion
Among
Various Number System
1.Binary
2.Octal
3.Decimal
4.Hexa-Decimal
Two’s Complement Number System
Question
Considering a 4 bit system, how to represent -5 in two’s complement number system?
Answer
There are multiple methods by which one can find out the answer.
Method 1: Take 1’s complement of +5 (represented in sign & magnitude) and add 1.
Method 2:- Scan from right to left, first 1 acts like a switch and then
complement rest of the bits.
Alternative Interpretation of
2’s Complement Numbers
Class Work
Example
Question:
What is the decimal equivalent of the number 1101 (represented in two’s complement)?
4 0100 -4 1100
+3 0011 + (-3) 1101
If carry-in to the high 7 0111
order bit = -7 11001
carry-out then ignore
carry
1 10001 -1 1111
-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
0100 1011
+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
0111 1000
5 0101 -7 1001
3 0011
-2 1100
-8 1000
7 10111
Overflow Overflow
0000 1111
5 0101 -3 1101
2 0010 -5 1011
7 0111 -8 11000
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 = xw–1 ,…, xw–1 , xw–1 , xw–2 ,…, x0
w
X • • •
k copies of MSB
• • •
X • • • • • •
k w
Sign Extension Example
U V
Product(P) 0110 6
3x-2=-6
0011 (Multiplicand)
1110 (Multiplier)
0000
0011
0011
0011
1111010 (Product)
Result of the addition of first 3 rows:- Now subtract the 4th row from the partial Sum:-
0000 010010
0011X 1101XXX
0011XX --------------
------------------ 1111010 (-6)
010010 (Partial Sum)
3 * (-2)
Multiplicand (N) 0011 3
Product(P) 1010 -6
Operation of the Algorithm
Take a look at the lsb of V
If it is 0 → do nothing
If it is 1 → Add N (multiplicand) to U
Right shift
Right shifting the partial product is the same as left
shifting the multiplicand, which
Needs to be done in every step
Last step is different
The Last Step ...
In the last step
lsb of V = msb of M (multiplier)
If it is 0 → do nothing
If it is 1
Multiplier is negative
Recall : A = A1 .. n-1 - 2n-1An
Hence, we need to subtract the multiplicand if the msb of
the multiplier is 1
Booth Multiplier
We can make our iterative multiplier faster
If there are a continuous sequence of 0s in the multiplier
do nothing
If there is a continous sequnce of 1s
do something smart
In case of Continuous 1’s:::::
If value of M is 0011 1100
Then,
Here, i = 2 and j = 5
So, M = 2^6 – 2^2
= 64 – 4
= 60
New method
Subtract the multiplicand when we scan bit i ( ! count starts from 0)
Keep shifting the partial product
Add the multiplicand(N), when we scan bit (j+1)
This process, effectively adds (2j+1 – 2i) * N to the partial product
Exactly, what we wanted to do …
So, (j – i + 1) additions will be replaced by 1 addition and 1 subtraction.
Operation of the Algorithm
Consider bit pairs in the multiplier
(current bit, previous bit)
Take actions based on the bit pair
Action table
Product(P) 0110 6
Multiplicand (N) 00011 3
Product(P) 1010 -6
Integer Division
Let us only consider positive numbers
N = DQ + R . . . . . (1)
N → Divident
D → Divisor
Q → Quotient
R → Remainder
Properties
[Property 1:] R < D, R >= 0
[Property 2:] Q is the largest positive integer
satisfying Equation 1
Dividend (N)
Divisor (D) 111 Quotient (Q)
11 10
11
01
Remainder (R)
Reduction of the Divison Problem
(U-D)
U V
Quotient(Q) 0010
Remainder(R) 0001
Restoring Division
Consider each bit of the dividend
Try to subtract the divisor from the U
register
If the subtraction is successful, set the relevant
quotient bit to 1
Else, set the relevant quotient bit to 0
Left shift