Because of hardware limitations, computers must represent everything with binary digits. It is customary to represent the sign with a bit placed in the leftmost position of the number.
If the binary number is signed, then the leftmost bit (or sign bit) represents the sign and the rest of the bits represent the number. If the binary number is assumed to be unsigned, then the leftmost bit is the most significant bit of the number.
In the signed-magnitude system, the number consists of a magnitude and a leftmost bit represents the sign, \(0\) for positive and \(1\) for negative. The signed-magnitude is used in ordinary arithmetic, but is awkward when employed in computer arithmetic because of the separate handling of the sign and the magnitude. Therefore, the signed-complement system is normally used.
In the signed-complement system, a negative number is indicated by its complement. The signed-complement system can use either the \(1\)’s complement or the \(2\)’s complement, but the \(2\)’s complement is the most common.
In the signed-complement system, using the \(2\)’s complement, the binary number with \(n\) bits will have a value that ranges from \(\left(-2^{n - 1}\right)\) to \(\left(2^{n - 1} - 1\right)\).
All possible four-bit signed binary numbers in the three representations.
Decimal (base-10) | Signed 2’s Complement | Signed 1’s Complement | Signed Magnitude |
---|---|---|---|
\(\displaystyle +7\) | \(\displaystyle 0111\) | \(\displaystyle 0111\) | \(\displaystyle 0111\) |
\(\displaystyle +6\) | \(\displaystyle 0110\) | \(\displaystyle 0110\) | \(\displaystyle 0110\) |
\(\displaystyle +5\) | \(\displaystyle 0101\) | \(\displaystyle 0101\) | \(\displaystyle 0101\) |
\(\displaystyle +4\) | \(\displaystyle 0100\) | \(\displaystyle 0100\) | \(\displaystyle 0100\) |
\(\displaystyle +3\) | \(\displaystyle 0011\) | \(\displaystyle 0011\) | \(\displaystyle 0011\) |
\(\displaystyle +2\) | \(\displaystyle 0010\) | \(\displaystyle 0010\) | \(\displaystyle 0010\) |
\(\displaystyle +1\) | \(\displaystyle 0001\) | \(\displaystyle 0001\) | \(\displaystyle 0001\) |
\(\displaystyle +0\) | \(\displaystyle 0000\) | \(\displaystyle 0000\) | \(\displaystyle 0000\) |
\(\displaystyle -0\) | \(\displaystyle -\) | \(\displaystyle 1111\) | \(\displaystyle 1000\) |
\(\displaystyle -1\) | \(\displaystyle 1111\) | \(\displaystyle 1110\) | \(\displaystyle 1001\) |
\(\displaystyle -2\) | \(\displaystyle 1110\) | \(\displaystyle 1101\) | \(\displaystyle 1010\) |
\(\displaystyle -3\) | \(\displaystyle 1101\) | \(\displaystyle 1100\) | \(\displaystyle 1011\) |
\(\displaystyle -4\) | \(\displaystyle 1100\) | \(\displaystyle 1011\) | \(\displaystyle 1100\) |
\(\displaystyle -5\) | \(\displaystyle 1011\) | \(\displaystyle 1010\) | \(\displaystyle 1101\) |
\(\displaystyle -6\) | \(\displaystyle 1010\) | \(\displaystyle 1001\) | \(\displaystyle 1110\) |
\(\displaystyle -7\) | \(\displaystyle 1001\) | \(\displaystyle 1000\) | \(\displaystyle 1111\) |
\(\displaystyle -8\) | \(\displaystyle 1000\) | \(\displaystyle -\) | \(\displaystyle -\) |
The signed-\(2\)’s-complement system has only one representation for \(0\), which is always positive. The other two systems have either a positive \(0\) or a negative \(0\), something not encountered in ordinary arithmetic. Note that all negative numbers have a \(1\) in the leftmost bit position.
Arithmetic
The rule for adding numbers in the signed-complement system does not require a comparison or subtraction, but only addition.
The addition of two signed binary numbers with negative numbers represented in signed-\(2\)’s-complement form is obtained from the addition of the two numbers, including their sign bits. A carry out of the sign-bit position is discarded.
\(\displaystyle (\pm A) - (+B) = (\pm A) + (-B)\)
\(\displaystyle (\pm A) - (-B) = (\pm A) + (+B)\)
where \(-A\) and \(-B\) are \(2\)’s complement of A and B, respectively.
Note that negative numbers must be initially in \(2\)’s complement form and that if the sum obtained after the addition is negative, it is in \(2\)’s complement form. Any carry out of the sign-bit position is discarded, and negative results are automatically in \(2\)’s complement form.
For example,
\(\displaystyle (+6) + (+13) = (00000110)_2 + (00001101)_2 = (00010011)_2 = +19\)
\(\displaystyle (-6) + (+13) = (11111010)_2 + (00001101)_2 = (00000111)_2 = +7\)
\(\displaystyle (-6) + (-13) = (11111010)_2 + (11110011)_2 = (11101101)_2 = -19\)
\(\displaystyle (+6) + (-13) = (00000110)_2 + (11110011)_2 = (11111001)_2 = -7\)
In order to obtain a correct answer, it is essential that the result has a sufficient number of bits.
If the sum of two \(n\)-bit numbers occupies \(n + 1\) bits, an overflow occurs.
Overflow is a problem in computers because the number of bits that hold a number is finite and fixed, and a result that exceeds the finite value by \(1\) cannot be accommodated.
Note that binary numbers in the signed-complement system are added and subtracted by the same basic addition and subtraction rules as unsigned numbers. Therefore, computers need only one common hardware circuit to handle both types of arithmetic.