Loading...
Two’s complement gives the leading bit a negative weight. Use that weighting to interpret a pattern rather than treating the sign as a separate minus symbol.
What you'll be able to do
For n-bit two’s complement, the most significant bit has weight -2ⁿ⁻¹ and the remaining bits have ordinary positive weights. The range is -2ⁿ⁻¹ to 2ⁿ⁻¹-1. There is one representation of zero and one extra negative magnitude.
An eight-bit pattern 11111010 represents -128+64+32+16+8+2=-6. Reading it as unsigned would instead give 250. The same bits can have different meanings, so the signedness must be specified. This qualification uses two’s complement for signed integer conversions; establish the width before interpreting a pattern.
Within a fixed width, invert every bit of a positive magnitude and add one to obtain its negative when that negative is representable. Keep all required leading bits. To compute A-B, add A to the representation of -B and retain the stated width.
A carry beyond the width is discarded in the modular representation, but it is not by itself a signed-overflow test. Adding two values with the same sign and obtaining the opposite sign indicates signed overflow. Check the intended mathematical result against the signed range.
The most negative n-bit value has no positive counterpart in the same width. Negating it in a fixed-width machine requires special care because its positive magnitude lies outside the range. Wider storage or an explicit overflow response may be needed.
When widening a two’s-complement value, sign extension repeats the leading sign bit; inserting zeroes would change many negative values. Arithmetic performed by a language with arbitrary-size integers may not overflow at eight bits automatically, so use an explicitly stated machine model in examination calculations.
| 8-bit subtraction 11-14 | Pattern |
|---|---|
| 11 | 00001011 |
| 14 | 00001110 |
| Invert 14 and add one | 11110010 |
| Add and retain 8 bits | 11111101 = -3 |
For eight-bit signed values, 100−(−40) has mathematical result 140, which does not fit −128 through 127. A wrapped pattern is not evidence that the exact signed answer fits. Conversely, a carry out from adding opposite-signed operands does not by itself prove signed overflow.
Write the intended signed calculation and range before performing modular bit arithmetic. Then decode the retained result and compare. This catches both an incorrect negation and a valid modular pattern that lies outside the task’s promised exact arithmetic domain.
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.