← Bits · guided lesson

Bit Manipulation

1 / 18 · story

the story

Two's complement integers

Every tool here exists because something was too slow or broke without it. Here is what.

1945 · John von Neumann (EDVAC report); the EDSAC team at Cambridge

In his June 1945 "First Draft of a Report on the EDVAC", von Neumann stored a negative number through the complements of its digits, working modulo a power of two. To subtract, every digit is replaced by its complement and one unit is added in the last place — what we now call two's complement. Cambridge's EDSAC (1949) stored its numbers in two's complement too. Java's language spec fixes it for good: every int is a 32-bit signed two's-complement integer.

the problem it solved

One adder for both addition and subtraction, and a single zero, instead of separate sign handling.

why it matters: without it

With sign-and-magnitude there would be two zeros (+0 and −0), and the CPU would need separate circuits to subtract and to compare signed numbers.

sources: von Neumann — First Draft of a Report on the EDVAC (1945), ed. Godfrey, IEEE Annals of the History of Computing (1993) ↗ · University of Cambridge Computer Laboratory — EDSAC simulator guide (number format) ↗ · Java Language Specification (Java SE 17), §4.2 Primitive Types and Values ↗

also in this chapter: Bitmasks as sets (and bitboards) · Clearing the lowest 1-bit: x & (x − 1) · XOR cancellation (parity) · Binary addition: sum bit and carry