complement form. So we must take 2’s complement of result obtained in step 2 to find correct magnitude of result.
2’s complement of result (1110)2 = (0010)2
so, final result = – (0010)2 = – (2)10 1.7.3 Signed Binary Representation
Untill now we have discussed representation of unsigned (or positive) numbers, except one or two places. In computer systems sign (+ve or –ve) of a number should also be represented by binary bits.
The accepted convention is to use 1 for negative sign and 0 for positive sign. In signed representation MSB of the given binary string represents the sign of the number, in all types of representation. We have two types of signed representation:
1. Signed Magnitude Representation 2. Signed Complement Representation
In a signed-magnitude representation, the MSB represent the sign and rest of the bits represent the magnitude. e.g.,
Note that positive number is represented similar to unsigned number. From the example it is also evident that out of 4-bits, only 3-bits are used to represent the magnitude. Thus in
general, n – 1 bits are used to denote the magnitude. So, the range of signed representation becomes –(2n–1 – 1) to (2n–1 – 1).
In a signed-complement representation the positive numbers are represented in true binary form with MSB as 0. Whereas the negative numbers are represented by taking appropriate complement of equivalent positive number, including the sign bit. Both 1’s and 2’s complements can be used for this purpose e.g.,
+5 = (0101)2
–5 = (1010)2 ←in 1’s complement = (1011)2 ←in 2’s complement
Note that in signed complement representation the fact remains same that n – 1 bits are used for magnitude. The range of numbers
In 1’s complement 0 to (2n–1 – 1) Positive Numbers – 0 to –(2n–1 – 1) Negative Numbers In 2’s complement 0 to (2n–1 – 1) Positive Numbers
– 1 to –2n–1 Negative Numbers
To illustrate the effect of these 3 representations, we consider 4-bit binary representation and draw the below table. Carefully observe the differences in three methods.
Decimal Signed 1’s complement 2’s complement Magnitude +0 0 0 0 0 0 0 0 0 0 0 0 0 +1 0 0 0 1 0 0 0 1 0 0 0 1 +2 0 0 1 0 0 0 1 0 0 0 1 0 +3 0 0 1 1 0 0 1 1 0 0 1 1 +4 0 1 0 0 0 1 0 0 0 1 0 0 +5 0 1 0 1 0 1 0 1 0 1 0 1 +6 0 1 1 0 0 1 1 0 0 1 1 0 +7 0 1 1 1 0 1 1 1 0 1 1 1 –8 — — 1 0 0 0 –7 1 1 1 1 1 0 0 0 1 0 0 1 –6 1 1 1 0 1 0 0 1 1 0 1 0 –5 1 1 0 1 1 0 1 0 1 0 1 1 –4 1 1 0 0 1 0 1 1 1 1 0 0 –3 1 0 1 1 1 1 0 0 1 1 0 1 –2 1 0 1 0 1 1 0 1 1 1 1 0 –1 1 0 0 1 1 1 1 0 1 1 1 1 –0 1 0 0 0 1 1 1 1 —
From the table, it is evident that both signed Magnitude and 1’s complement methods introduce two zeros +0 and – 0 which is awkward. This is not the case with 2’s complement. This is one among the reasons that why all the modern digital systems use 2’s complement method for the purpose of signed representation. From the above table, it is also evident that in signed representation2
2
n
positive numbers and2
2
n
negative numbers can be represented
with n-bits. Out of 2n combinations of n-bits, first2
2
n
combinations are used to denote the positive numbers and next2
2
n
combinations represent the negative numbers.
Example 1. In a signed representation given binary string is (11101)2. What will be the sign and magnitude of the number represented by this string in signed magnitude, 1’s complement and 2’s complement representation.
Solution.
The number N = (11101)2
since MSB = 1 the given number is negative.
(i) In signed Magnitude MSB denotes sign and rest of the bits represent magnitude. So,
(ii) In 1’s complement if number is negative (i.e., MSB = 1) then the magnitude is obtained by taking 1’s complement of given number.
1’s complement of (11101)2 = (00010)2
so, (11101)2 = –2 in 1’s complement.
(iii) In 2’s complement if number is negative (i.e., MSB = 1) then magnitude is obtained by taking 2’s complement of given number.
2’s complement of (11101)2 = (00011)2 = 3
so, (11101)2 = –3 in 2’s complement.
Example 2. Obtain an 8-bit representation of –9 in signed Magnitude, 1’s complement and 2’s complement representation.
Solution. We first find binary of 9 i.e., (9)10 = (1001)2 Next we represent 9 using 8-bits. So, N = (00001001)2
= (9)10
(i) In signed Magnitude, MSB shows sign and rest of the bits shows true magnitude. So, (–9)10 = (10001001)2
(ii) In 1’s complement, negative number is represented by taking 1’s complement of positive number. So,
(–9)10 = 1’s complement of (00001001)2 = (11110110)2
(iii) In 2’s complement
(–9)10 = 2’s complement of (00001001)2 = (11110111)2
1.7.4 Arithmetic Overflow
When the result of an arithmetic operation requires n+1 bits, upon operating on n-bits number, an overflow occurs. Alternately, if result exceeds the range 0 to 2n – 1, an overflow occurs.
Let us consider the addition of two 4-bit numbers
Thus, addition of two 4-bits numbers requires 5-bits (n+1 bits) to represent the sum. Alternately, the result of addition of 4-bits, falls outside the range 0 to 15 (i.e., 0 to 24–1).
Thus, overflow has occured.
In case of signed arithmetic the overflow causes the sign bit of the answer to change. In this case an overflow occurs if the result does not lie in the range –2n–1 to 2n–1 – 1. In signed arithmetic overflow can occur only when two positive numbers or two negative num- bers are added.
Let us consider 4-bit signed 2’s complement representation. 1. Addition of two positive numbers +6 and +5
Since, MSB of result is 1, if reflects a negative result which is incorrect. It happened because overflow has changed the sign of result.
2. Addition of two negative numbers –6 and –5
In 2’s complement if a carry is generated after the addition then carry is discarded and result is declared positive. Thus, result = (0101)2 = +5 which is wrong, because addition of two negative numbers should give a negative result. This happened due to overflow.
Note that overflow is a problem that occurs when result of an operation exceeds the capacity of storage device. In a computer system, the programmer must check the overflow after each arithmetic operation.
1.7.5 9’s and 10’s Complement
9’s and 10’s complements are the methods used for the representation of decimal num- bers. They are identical to the 1’s and 2’s complements used for binary numbers.
9’s complement: 9’s complement of a decimal number is defined as (10n – 1) – N, where n is no. of digits and N is given decimal numbers. Alternately, 9’s complement of a decimal number can be obtained by subtracting each digit from 9.
9’s complement of N = (10n–1) – N.
Example 1. Find out the 9’s complement of following decimal numbers.
(i) 459 (ii) 36 (iii) 1697
Solution. (i) By using (10n–1) – N; But, n = 3 in this case So, (10n–1) – N = (103 – 1) – 459 = 540
Thus, 9’s complement of 459 = 540 (ii) By subtracting each digit from 9
9 9 –3 6 6 3 So, 9’s complement of 36 is 63. (iii) We have N = 1697, so n = 4 Thus, 10n–1 = 104 – 1 = 9999 So, (10n–1) – N = (104–1) – 1697 = 9999 – 1697 = 8302 Thus, 9’s complement of 1697 = 8302
10’s complement: 10’s complement of a decimal number is defined as 10n – N. 10’s complement of N = 10n – N
but 10n – N = (10n – 1) – N + 1
= 9’s complement of N + 1
Thus, 10’s complement of a decimal number can also be obtained by adding 1 to its 9’s complement.
Example 2. Find out the 10’s complement of following decimal numbers. (i) 459 (ii) 36. Solution. (i) By using 10n – N; We have N = 459 so n = 3
So, 10n – N = 103 – 459 = 541
So, 10’s is complement of 459 = 541
(ii) By adding 1 to 9’s complement
9’s complement of 36 = 99 – 36 = 63 Hence, 10’s complement of 36 = 63 + 1
= 64
1.7.6 r’s Complement and (r – 1)’s Complement
The r’s and (r – 1)’s complements are generalized representation of the complements, we have studied in previous subsections. r stands for radix or base of the number system, thus
r’s complement is referred as radix complement and (r – 1)’s complement is referred as diminished radix complement. Examples of r’s complements are 2’s complement and 10’s
In a base-r system, the r’s and (r – 1)’s complement of the number N having n digits, can be defined as:
(r – 1)’s complement of N = (rn – 1) – N
and r’s complement of N = rn – N
= (r – 1)’s complement of N + 1
The (r – 1)’s complement can also be obtained by subtracting each digit of N from
r–1. Using the above methodology we can also define the 7’s and 8’s complement for octal
system and 15’s and 16’s complement for hexadecimal system. 1.7.7 Rules for Subtraction using (r–1)’s Complement
Let M (minuend) and S (subtrahend) be the two numbers to be used to evaluate the difference D = M – S by (r – 1)’s complement and either or both the numbers may be signed or unsigned.
Until and unless specified the given rules are equally applied for both signed and un- signed arithmetic. For the clarity of process, let us assume that two data sets are:
Unsigned data— Mu = 1025, Su = 50 and Du = Mu – Su Signed data— Ms = –370, Ss = 4312 and Ds = Ms – Ss