−45 in 8-bit two's complement

introinvert and add one

Answer

11010011

Why this example is worth doing

The standard procedure: write the magnitude in binary, invert every bit, add one. The page shows all three stages rather than the answer, because the marks are for the stages, and it verifies the result by adding it back to +45 to get zero with the carry discarded. That check is worth teaching as a habit — it catches an off-by-one in the increment step immediately.

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

-45 in 8-bit two's complement

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

|-45| in binary00101101

invert every bit11010010

add 111010011invert-and-add-one is the negation rule

8-bit pattern1101 00110xD3

check11010011 reads 211 unsigned and -45 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