ELEC353 S. al Zahir UBC Sign-Magnitude Representation High order bit is sign: 0 = positive (or zero), 1 = negative Low order bits represent the magnitude: 0 (000 2 ) thru 7 (111 2 ) Number range for n bits = ± (2 n -1) Two representations for 0
ELEC353 S. al Zahir UBC Sign-Magnitude Representations Addition and Subtraction of Numbers (-3) Both operands have same sign: Result sign bit is the same as the operands' sign Operands have different sign: Operation is subtraction. Sign of result depends on sign of number with the larger magnitude R = X-Y if X>Y subtract |Y| from |X|, sign(R) = sign(X). if X<Y subtract |X| from Y|, sign(R) = sign(Y).
ELEC353 S. al Zahir UBC Ones Complement Representation Disadvantages of Sign-Magnitude Representation: Cumbersome addition/subtraction Must compare magnitudes to determine sign of result Ones Complement Representation: Definitions N is positive number, then N is its negative 1's complement N = (2 - 1) - N n Example: 1's complement of 7 = -7 in 1's comp. Shortcut method: simply compute bit wise complement > 1000 = -1 in 1's comp. 2 = = = =
ELEC353 S. al Zahir UBC Ones Complement Representation Subtraction implemented by addition using 1's complement Still two representations of 0! This causes some problems Some complexities in addition
ELEC353 S. al Zahir UBC Two Complement Representations Only one representation for 0 One more negative number than positive number Similar to 1's complement except shifted by one position clockwise N* =2’s comp of N N* = 2 n - N
ELEC353 S. al Zahir UBC Two Complement Representations N* = 2 - N n Example: Twos complement of 7 2 = = = repr. of -7 Example: Twos complement of = = = repr. of 7 4 sub Shortcut method: Twos complement = bitwise complement > > 1001 (representation of -7) > > 0111 (representation of 7)
ELEC353 S. al Zahir UBC Number Systems Ones Complement Calculations (-3) End around carry Add carry-out of sign-bit to LSB (assuming no overflow) Method: 1- Add two n-bit numbers starting from LSB 2- If there is an end carry (into position n+1), then remove carry and add a 1 to the LSB (end-around carry).
ELEC353 S. al Zahir UBC Number Systems Ones Complement Calculations Why does end-around carry work? Its equivalent to subtracting 2 n and adding 1 (i.e. performing -2 n +1) Case 1 (M > N): S = M - N = M + N = M + (2 n N) = (M - N) + 2 n - 1 After end-around carry SUM = S -2 n +1= (M - N) + 2 n n + 1 = M - N Case 2 (-M-N, where M + N < 2 n-1); -M + (-N) = M + N = (2 n - M - 1) + (2 n - N - 1) = 2 n + [2 n (M + N)] - 1 after end around carry: = 2 n (M + N) this is the correct form for representing -(M + N) in 1's complement -4 + (-3)
ELEC353 S. al Zahir UBC Number Systems Twos Complement Calculations (-3) Overflow condition for 2’s complement: if carry into sign-bit differs from carry out of sign-bit then the results is an overflow Simpler addition scheme makes twos complement the most common choice for integer number systems within digital systems Method (If no overflow): 1- Add two n-bit numbers starting from LSB 2- Discard the carry out of the sign-bit.
ELEC353 S. al Zahir UBC Number Systems Twos Complement Calculations Discarding End Carry: is equivalent to subtracting 2 n Case 1 (-M+N, where N > M): S = -M + N = M* + N = (2 n - M) + N = 2 n - (N - M) After discarding end carry SUM = S -2 n = 2 n - (N - M) - 2 n = N - M Case 2 (-M + (-N), where M + N < 2 n-1 ): S = -M + (-N) = M* + N* = (2 n - M) + (2 n - N) = 2 n - (M+N) + 2 n After discarding end carry SUM = S -2 n = 2 n - (M+N) + 2 n - 2 n = 2 n - (M+N) = (M+N)* -4 + (-3)
ELEC353 S. al Zahir UBC Overflow Conditions (2’s Complement) Overflow occurs when the result of adding two positive (or two negative) n-bit numbers requires more than n bits to be expressed correctly. Overflow indications: Adding two positive numbers results in a negative number Adding two negative numbers results in a positive number = =
ELEC353 S. al Zahir UBC Overflow Examples Overflow No overflow Simple Check: Overflow occurs when carry into sign-bit does not equal the carry out of the sign-bit.
ELEC353 S. al Zahir UBC Logic Circuits for Binary Addition Half Adder Circuit Half-adder Schematic With twos-complement numbers, adders can perform both addition and subtraction.
ELEC353 S. al Zahir UBC Logic Circuits for Binary Addition Full Adder Cascaded Multi-bit Adder usually interested in adding more than two bits this motivates the need for the full adder