Sign and Magnitude in a Bit Pattern

Sign-and-magnitude encoding reserves a bit for the sign and uses the remaining bits as an unsigned magnitude.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Work a short example
  2. Account for two zero spellings
  3. Separate one sign position from magnitude bits
  4. Account for two zero encodings in comparisons

Work a short example

With one sign bit and three magnitude bits, 1011 means minus three. The same pattern means minus five under four-bit two's complement. Read the format's interpretation before converting to a language integer.

Account for two zero spellings

The patterns 0000 and 1000 both have zero magnitude. A serializer may choose one canonical form or preserve the sign if the format gives it meaning. Parsing and formatting separates those decisions.[1]

Separate one sign position from magnitude bits

In a five-bit sign-magnitude model, 10110 has a negative sign and magnitude six. The positive six pattern is 00110. Their magnitude bits agree. This differs from applying a negative weight to the top position. See two's complement interpretation.

Magnitude weights: 8,4,2,1
00110 → +6
10110 → -6
00000 and 10000 encode signed zeros

Account for two zero encodings in comparisons

Software comparing bit patterns can distinguish positive and negative zero even if the domain's mathematical comparison merges them. Decide whether a serializer canonicalizes zero. The magnitude has maximum fifteen in this example, so minus sixteen is outside this model.

References

  1. Python Software Foundation, Python Standard Library: Built-in Types ↩