2.3 Unsigned, Signed, and Two's-Complement Integers
Section 2.2 converted values among bases. A stored bit pattern, however, does not carry a label saying whether it is positive or negative. Hardware and software must agree on an integer encoding.
Consider the 8-bit pattern 11111111. It can mean:
- unsigned integer 255;
- sign-magnitude integer −127;
- ones'-complement negative zero;
- two's-complement integer −1.
The bits are identical. Only the interpretation contract changes.
Unsigned integers use every bit as a positive weight
For an -bit unsigned integer:
The range is:
An 8-bit unsigned integer therefore ranges from 0 to 255. Its highest bit has positive weight .
Unsigned encoding is appropriate for quantities that cannot be negative, such as a byte count or memory address offset—provided the maximum value is sufficient.
Three ways to encode a sign
Sign-magnitude
The highest bit is a sign indicator: 0 for positive and 1 for negative. The remaining bits store magnitude.
For 8 bits:
00000101means +5;
10000101means −5;
- both
00000000and10000000represent zero.
Two zero patterns complicate comparisons and arithmetic.
Ones' complement
A negative value is formed by inverting every bit of its positive pattern.
| One's-complement value | 8-bit pattern |
|---|---|
00000101 | |
11111010 |
It also has two zeros: all zeros for +0 and all ones for −0.
Two's complement
A negative value is formed by inverting the positive magnitude and adding one within the fixed width.
For +5 in 8 bits:
Thus 11111011 represents −5.
Two's complement has one zero and lets the same binary adder handle both positive and negative addition. It is the dominant signed-integer encoding in modern processors.
Lab 1 — keep the bits fixed and change the contract
Construct three important 8-bit patterns and compare their unsigned, sign-magnitude, ones'-complement, and two's-complement values.
Pay special attention to all ones and to a pattern containing only the highest bit. These boundary patterns reveal the different range and zero rules.
Reading a two's-complement pattern directly
For bits, the highest bit has weight while all lower bits keep positive weights:
Decode 11111011:
An equivalent shortcut is:
1. interpret the whole pattern as unsigned;
2. if the highest bit is 1, subtract .
For 11111011, the unsigned value is 251, so .
Range asymmetry
An -bit two's-complement integer ranges from:
The 8-bit range is −128 through +127. There is one extra negative value because zero occupies one nonnegative pattern.
| Pattern | 8-bit two's-complement value |
|---|---|
00000000 | 0 |
00000001 | 1 |
01111111 | 127 |
10000000 | −128 |
11111111 | −1 |
The value −128 has no positive 8-bit signed counterpart. Negating it within eight bits produces the same pattern because +128 is outside the range. This boundary matters when writing arithmetic code.
Lab 2 — forge a negative pattern
Begin with an 8-bit magnitude. Invert, add one, or toggle individual bits until the working pattern decodes to the requested negative target.
Try −1 and −128 after ordinary values. Explain why their final patterns are all ones and 10000000, respectively.
Width changes require sign extension
When increasing the width of an unsigned value, add zeros on the left:
When increasing the width of a two's-complement value, copy the sign bit on the left. This is sign extension:
Appending zeros on the left of a negative two's-complement value would change its sign and value.
Reducing width is safe only if the removed high bits are redundant sign-extension bits. Always decode or range-check before truncating.
Checkpoint
Keep representation separate from interpretation:
- unsigned uses only positive powers of two;
- sign-magnitude stores sign and magnitude separately;
- ones' complement forms negatives by bit inversion and has two zeros;
- two's complement forms negatives by inversion plus one, has one zero, and ranges from to .
The next section examines what happens when an arithmetic result does not fit, then contrasts binary integers with BCD and character encodings.