Sign-and-Magnitude
This representation of negative number has its inspiration from the mathematical representation of negative number. We write negative number mathematically with a minus sign in front of the number. For instance, -123 is negative 123.
Since a computer works with binary representation, we can see clearly that there are only two possible choice for the signs, namely negative (i.e., -) and non-negative (i.e., +)1.
Representation
Using bits, we can simply have a simple convention:
Sign | Symbol | Bit |
---|---|---|
Positive | + | 0 |
Negative | - | 1 |
This poses a slight problem. Where should we put this sign bit?
If we follow the mathematical convention, then it should be before the actual number, which we call the magnitude. However, if we also follow the usual convention that leading zeroes do not carry any value, then how do we know that we have actually reached the sign bit? For instance, since the binary 100101 should be equivalent to 000000100101 numerically, if we add the sign bit in front, then we now have two different possibilities for their negative version:
Sign | Magnitude | Representation |
---|---|---|
1 | 100101 | 1100101 |
1 | 000000100101 | 1000000100101 |
Negative Sign-and-Magnitude Complement
Flip left-most bit
Problems
This is clearly problematic. Unfortunately, the solution might not be satisfactory. We simply have to limit the number of bits for the magnitude. So, representing negative numbers requires us to specify the number of bits to be used. Although this solve the problem of determining where the sign bit is, it also poses additional problems. Consider a sign-and-magnitude representation using a total of 8 bits (1 sign bit and 7 magnitude bit).
- Redundant Bit: Representing small numbers such as 0 would still require 8 bits instead of 1 bit.
- Limit: Representing large positive/negative numbers may be impossible due to the limited number of bits.
- Redundant 0: Representing zero can be ambiguous as there are two possible zeroes: +0 which is 00000000 and -0 which is 10000000. However, both zeroes should have equal value.
Benefits
The benefit of this representation is its simplicity and its close relation with mathematical notation. Another more practical benefit is that we can negate any number (i.e., convert a positive number into its negative number) using a single bit flip.
- Negate (00100001)sm2 by flipping the first bit into (10100001)sm.
- Negate (10000101)sm by flipping the first bit into (00000101)sm.
In both examples, we do not even need to know what the decimal values of those numbers are.
Quick Quiz
What are the following values in base 10?
- (00100001)sm
- (10100001)sm
- (10000101)sm
- (00000101)sm
- (+33)10
- (-33)10
- (-5)10
- (+5)10
Unfortunately, that's the only operations that can be done easily using this representation. It is not clear how we can easily add two numbers using this representation.
Range
8 bits
We can compute the range of any sign-and-magnitude representation. First, let us compute the range for 1 sign bit and 7 magnitude bit.
Kind | Representation | Decimal |
---|---|---|
Largest | 01111111 | (+127)10 |
Smallest | 11111111 | (-127)10 |
So, the range of is from (-127)10 to (+127)10 which contains 255 numbers. This is a little bit strange because if we simply use 8 bits without the sign bit, the range is from 0 to 255 which contains 256 numbers. So where is the missing number?
This is basically due to the redundant 0. Remember, we have two zeroes:
Kind | Representation | Decimal |
---|---|---|
Positive 0 | 00000000 | (+0)10 |
Negative 0 | 10000000 | (-0)10 |
n bits
We can now extend this for any number of bits. Let us consider n number of bits such that one of them is used as sign bit. Therefore, we have 1 sign bit and (n-1) magnitude bits.
Kind | Representation | Decimal |
---|---|---|
Largest | 0111...11 | (+2n-1-1)10 |
Smallest | 1111...11 | (-2n-1-1)10 |
Steps
If you are still not sure how do we get (2n-1-1)10, we simply do the following computation using an arbitrary value of n. First, note that we only have (n-1) values of 1. Second, since the position starts from 0, the largest position is (n-2).
Working
(111...11)2
⇒ 1 × 2n-2 + 1 × 2n-3 + 1 × 2n-4 + ... + 1 × 21 + 1 × 20
⇒ 2n-2 + 2n-3 + 2n-4 + ... + 21 + 20
To find the closed-form solution of the summation above, we simply let S = 2n-2 + 2n-3 + 2n-4 + ... + 21 + 20. Then
S
S = 2n-2 + 2n-3 + 2n-4 + ... + 21 + 20
2S
2S = 2 × (2n-2 + 2n-3 + 2n-4 + ... + 21 + 20)
2S = 2 × 2n-2 + 2 × 2n-3 + 2 × 2n-4 + ... + 2 × 21 + 2 × 20
2S = 2n-1 + 2n-2 + 2n-3 + ... + 22 + 21
2S - S = S
2S - S = (2n-1 + 2n-2 + 2n-3 + ... + 22 + 21) - (2n-2 + 2n-3 + 2n-4 + ... + 21 + 20)
2S - S = (2n-1 + 2n-2 + 2n-3 + ... + 22 + 21) - (2n-2 + 2n-3 + 2n-4 + ... + 21 + 20)
2S - S = 2n-1 - 20
S = 2n-1-1
So, the range is 2n-1 for any n bits sign-and-magnitude numbers. Clearly, this is not optimal. We will try to improve this in later representation.