FREE LESSON · Digital logic · 6 OF 8

One bit pattern, several number systems

One bit pattern, several number systems: Track width, signedness, rounding, and overflow explicitly.

Prerequisites: Binary positional notation and addition.

Learning objectives

  • Decode fixed-width two’s-complement values.
  • Distinguish carry, signed overflow, and rounding.
  • Explain why floating-point addition depends on order.

Representation supplies an interpretation

Bits alone do not specify a number. The byte 11111110 represents unsigned 254, two’s-complement signed −2, or part of a floating-point encoding. A type or instruction supplies the interpretation. In an unsigned w-bit format, values range from 0 to 2^w−1. Two’s-complement assigns the highest bit weight −2^(w−1), giving the range −2^(w−1) through 2^(w−1)−1.

For four bits, 1101 means −8+4+1=−3. Addition of bit patterns naturally produces a result modulo 16 if only four result bits are retained. Thus 1101+0101 produces 0010 after discarding the carry: interpreted as signed numbers, −3+5=2. But 0111+0001 produces 1000, which means −8 rather than mathematical 8. The latter is signed overflow. Carry out and signed overflow answer different questions.

def signed(bits, width):
    assert 0 <= bits < 2 ** width
    return bits - 2 ** width if bits >= 2 ** (width - 1) else bits
assert signed(0b1101, 4) == -3
assert signed((7 + 1) & 0b1111, 4) == -8

Floating point trades uniform spacing for range

A normal binary floating-point value has a sign, a significand, and a power-of-two scale. With p significant binary digits, representable values in a given power-of-two interval have fixed spacing, and that spacing doubles in the next interval. Rounding selects a nearby representable value. This is a representation effect, not random noise.

Use a toy format with three significant bits and rounding to nearest, ties to even. Near 8, adjacent values are 8, 10, 12, and 14. The exact result 8+1=9 lies halfway between 8 and 10 and rounds to 8 because its retained low bit is even. Therefore (8+1)+1 evaluates to 8 in this toy arithmetic, while 8+(1+1) evaluates to 10. Real binary formats have more precision but the same structural issue.

IEEE 754 formats also define signed zeros, infinities, NaNs, and subnormal values. Subnormals provide gradual behavior near zero with reduced precision. An underflow boundary differs from an integer wraparound boundary.

Follow the language contract

Some languages define modular integer arithmetic, some detect overflow, and some leave particular overflow cases undefined. Python integers grow instead of automatically wrapping to a machine word; the mask above deliberately simulates a four-bit register. Never infer source-language behavior from a hardware adder alone. For numerical work, specify acceptable error, scale inputs sensibly, and avoid equality assumptions that require every intermediate real number to be exactly representable.

Primary reading

Practice problem

Decode the eight-bit pattern 10000001 as unsigned and signed. Compute the retained eight-bit result of 127+1 and decide whether signed overflow occurred. Then explain why storing more exponent bits, without more significand bits, does not necessarily fix the toy example 8+1.

Worked solution

Unsigned 10000001 is 128+1=129. Signed two’s-complement assigns the top bit weight −128, so the same pattern is −127. The retained result of 127+1 is 10000000. This is unsigned 128 but signed −128. Both operands were nonnegative and the mathematical sum lies outside the signed range −128 through 127, so signed overflow occurred. Extra exponent bits extend the range of scales that can be represented. They do not increase the count of significant digits at a given scale. The toy calculation loses the added one because representable values around eight are spaced two units apart. Additional significand precision, or a different numerical arrangement justified by the algorithm, addresses that loss.

Check your understanding

Which four-bit sum has signed overflow but no carry out?

  • 1111 + 0001
  • 0111 + 0001
  • 1101 + 0001
  • 0001 + 0010
Explanation

7+1 produces 1000 without a fifth bit, but 8 is outside the signed four-bit range.

Why is regrouping floating-point additions potentially observable?

  • The exponent is always random.
  • Every addition rounds to the available representable values.
  • Floating point cannot represent any integer.
  • Multiplication changes addition into subtraction.
Explanation

Intermediate rounding can differ between groupings even when exact real arithmetic would agree.

Primary reading

Open the study and save your work →