Loading...
The same bits can represent different signed values under different conventions. Write the convention and width first, then decode the pattern using the correct weights.
What you'll be able to do
In sign and magnitude, the leading bit records the sign, commonly zero for non-negative and one for negative. The remaining bits encode an unsigned magnitude. With eight total bits, 00000101 is +5 and 10000101 is −5. Changing the sign bit changes the sign without complementing the magnitude field.
Seven magnitude bits encode 0 through 127, so the eight-bit range is −127 through +127. There are two representations of zero: 00000000 and 10000000. The latter is a negative-zero encoding, not −128. A question may specify how zero is treated operationally, but the two bit patterns explain why this range differs from two’s complement.
In eight-bit two’s complement, the leading bit has weight −128 and the other positions have weights 64 through 1. Thus 10000101 means −128+5=−123, even though the identical pattern means −5 in sign and magnitude. The width is essential because it determines the leading negative weight.
To encode −5 in eight-bit two’s complement, write +5 as 00000101, invert to 11111010 and add one to obtain 11111011. Two’s complement has one zero and an asymmetric range, −128 through +127. Negating the most negative value cannot produce an in-range positive value of the same width.
Binary addition with the usual fixed-width rules naturally supports two’s-complement values modulo the field width, with a separate check for signed overflow. For example +5 plus −5 becomes 00000101+11111011=1 00000000; discard the carry beyond eight bits and obtain zero. The discarded carry alone does not diagnose signed overflow.
Do not apply that same operation blindly to sign-and-magnitude words. Adding 00000101 and 10000101 as ordinary unsigned words gives 10001010, which would mean −10 in sign and magnitude, although +5+(−5)=0. A sign-and-magnitude arithmetic implementation must account for signs and compare or combine magnitudes appropriately. The representation and the arithmetic mechanism must agree.
| Pattern | 8-bit sign and magnitude | 8-bit two’s complement |
|---|---|---|
| 00000101 | +5 | +5 |
| 10000101 | −5 | −123 |
| 11111011 | −123 | −5 |
| 10000000 | Negative zero | −128 |
Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.