+127 and the sign-bit boundary — two's complement

coreone more and it wraps

Answer

01111111

Why this example is worth doing

0111 1111 is the largest positive eight-bit value; adding one gives 1000 0000, which reads as −128. The page shows the wrap as an arithmetic fact rather than an error, and points out that the hardware does not know anything went wrong — the same bits are a perfectly good unsigned 128. Signedness is an interpretation the programmer supplies, not a property of the register.

Try your own input in the Two’s complement. Encode and decode signed binary at any width, with overflow and sign extension.

How the answer is reached

127 in 8-bit two's complement

Encode 127 in 8-bit two's complement. Representable range: -128 to 127.

127 in binary01111111Non-negative values are plain binary, zero-padded to the width.

8-bit pattern0111 11110x7F

check01111111 reads 127 unsigned and 127 signed

Compare with

Open this example in the Two’s complement

The field arrives filled in with this example’s input.

Note:

Notation this page assumes

  • Bit strings are written most significant bit first, and bit 0 is the least significant bit.
  • A width is stated explicitly wherever it changes the answer; nothing is silently sign-extended or truncated.
  • Negative values are two’s complement, not sign–magnitude, and the most significant bit is the sign bit.

Sources

  • Knuth, The Art of Computer Programming, Vol. 2, §4.1 “Positional Number Systems” (1997)
  • IEEE 754-2019, Standard for Floating-Point Arithmetic