binary arithmetic stephen boyd march 14, 2011. two's complement most significant bit represents...
Post on 27-Dec-2015
216 Views
Preview:
TRANSCRIPT
Two's Complement
Most significant bit represents sign .
0 = positive
1 = negative Positive numbers behave exactly like unsigned
integers. Negative numbers are added to the negative
value of the most significant bit.
1 101 = (-8) + 5 = -3
1 001 1011 = (-128) + 27 = -101
1 111 1111 = (-128) + 127 = -1
Two's Complement
To convert between negative and positive, invert every bit and add 1.
1 101 (-3) → 0 010 + 1 = 0 011 (3)
0 011 (3) → 1 100 + 1 = 1 101 (-3) For n number of bits, an integer has a possible
range of -2n-1 to 2n-1 -1.
1 000 to 0 111 for a 4-bit integer, or -8 to 7
Binary Addition and Subtraction
Similar to decimal addition
3 + 2 → 0 011 + 0 01000 10 carry
0 011 3
0 010 2
0 101 = 5
Subtraction can be achieved by adding a negative.
3 – 5 = 3 + (-5)
Overflow
Overflow occurs when the result is beyond the possible range allowed by the number of bits.
4 + 5 → 0 100 + 0 10101 00 carry
0 100 4
0 101 5
1 001 = -7 (WRONG!)
Typically only occurs when the two left-most carries are either 10 or 01. 11 produces no problems.
Can be detected with a simple XOR of the last two carries.
Half Adders
A simple addition of 1-bit operands (A and B) produces two values:
The summation S = A XOR B
The carry C = A AND B This type of circuit is called a half adder:
Full Adders
The drawback of half adders is that they do not account for the carry-in from a previous bit addition. For that, there are full adders:
S = A XOR B XOR Cin
C = (A AND B) OR (Cin AND (A XOR B))
Ripple Adders
Multiple full adders can be strung together to create a ripple adder, with the carry-out from each going into the carry-in of the next.
Carry Lookahead Adders
A problem with the ripple adder is that each 1-bit adder must wait for the previous adder to determine if there will be a carry before completing the summation.
Lookahead adders are able to determine simultaneously for each 1-bit adder if there will be an incoming carry, and then allow each adder to perform the summation without waiting.
Carry Lookahead Adders
In lookahead adders each 1-bit adder first informs the look ahead logic of two values:
G – If the bit will generate a carry. This will happen regardless of an incoming carry if both A and B are 1. (G = A AND B)
P – If the bit will propagate an incoming carry into the next bit adder. This will happen if either A or B is at least 1. (P = A OR B)
Once P and G are known for all bit adders, it is possible to determine incoming carries without waiting for the full summation.
Subtraction
As stated, a two's complement integer can be negated by inverting each bit and adding 1.
X XOR Y will return Y if X = 0, and Y' if X = 1. With that in mind, any adder can be adapted to subtract instead with a toggle input XOR'd with each of the input bits and carried in to the first 1-bit adder.
Multiplication of Unsigned Integers
Again, similar to decimal. Multiplying integers with x and y number of bits
will yield a product of bits x+y. (at most)
Example w/4-bit integers: 12 * 5 = 60
1 1 0 00 1 0 11 1 0 0
1 1 0 0 1 1 1 1 0 0
Multiply the numbers as usual for decimal, then add them. Because binary only uses1s and 0s, a shortcut is to simply repeat themultiplicand for each 1 in the multiplier, shifted left.
Multiplicand
Multiplier
Product
Fast Multiplication: Booth's Algorithm
Multiplying integers M * N of lengths m and n respectively, need the following values:
A = M followed by n+1 0's.
B = M' followed by n+1 0's.
P = m 0's followed by N and a 0. Start with P and examine 2
rightmost bits.10 → P = P+B, shift right
01 → P = P+A, shift right
00 or 11 → shift right Do this n times, then drop the
right most bit. P is now the product in two's complement.
Example: -5 * 7 → 1011 * 0111A = 1011 0000 0
B = 0101 0000 0
P = 0000 0111 0
1) P=P+B 0 0 0 0 0 1 1 1 0 shift 0 1 0 1 0 1 1 1 02) shift 0 0 1 0 1 0 1 1 13) shift 0 0 0 1 0 1 0 1 14) P=P+A 0 0 0 0 1 0 1 0 1 shift 1 0 1 1 1 0 1 0 1
Product = 1 1 0 1 1 1 0 1
top related