Sign-Magnitude
Sign-magnitude stores a number as a sign bit plus an ordinary magnitude, mirroring how people write negatives.
The layout
In sign-magnitude the top bit is a pure sign flag — 0 for positive, 1 for negative — and the remaining bits hold the magnitude as a plain unsigned number. The 8-bit pattern 10000101 reads as −5.
Human familiarity
This is the closest binary analog to writing a minus sign in front of a number. It is easy to read, which is why it appears in the significand of floating-point formats.
Two zeros
Like one's complement, sign-magnitude has both +0 (00000000) and −0 (10000000), which requires extra handling in comparisons and arithmetic.
Awkward arithmetic
Adding two sign-magnitude numbers requires inspecting the signs first and then deciding whether to add or subtract magnitudes. This branching makes the adder more complex than the uniform two's complement adder.
Modern role
General-purpose integer arithmetic uses two's complement, but IEEE-754 floating point uses sign-magnitude for its mantissa, giving it a signed zero and a symmetric range of positive and negative values.
| Bits (8-bit) | Value |
|---|---|
| 00000101 | +5 |
| 10000101 | -5 |
| 00000000 | +0 |
| 10000000 | -0 |
def sign_mag(bits, n):
sign = -1 if bits & (1 << (n-1)) else 1
mag = bits & ((1 << (n-1)) - 1)
return sign * mag
print(sign_mag(0b10000101, 8)) # -5