Binary Arithmetic & Two's Complement Lab
Visualize binary bit weights, explore signed representations, and step-by-step column arithmetic (addition, subtraction, and multiplication).
the logic of negative numbers in binary 🖖
In modern computers, negative integers are represented using Two's Complement. Under this representation, the most significant bit (MSB) simply acts as a negative weight: for an 8-bit integer, bit 7 represents -128 instead of +128. This mathematical mapping has a beautiful property: subtraction is identical to addition. For example, to calculate A - B, the CPU simply calculates A + (~B + 1), where ~B is the bitwise complement. This eliminates the need for separate subtraction hardware, allowing the Arithmetic Logic Unit (ALU) to use the same logic gate adder circuits for both addition and subtraction. One's Complement and Sign & Magnitude were historically used, but both suffer from duplicate representations of zero (positive and negative zero) and require more complex ALU control logic.
binary is just place value, base two 🖖
In everyday numbers each column is worth ten times the one to its right; in binary the factor is simply 2. So the bits carry (from the right) the weights 1, 2, 4, 8, 16, 32, … Reading a binary number means adding up the weights wherever a 1 sits: 1011 is 8 + 0 + 2 + 1 = 11. The tool's bit-weight display lets you toggle each bit and watch the running total, which is the secret behind every conversion here.
your CPU multiplies like a Russian peasant 🖖
The long multiplication here — doubling A and adding it wherever B has a 1 bit — is exactly "Russian peasant multiplication," a method already found on Egyptian papyri over 3000 years old. You halve one number (dropping any remainder) and double the other, then sum the doubled values wherever the halved number is odd. Halving and checking for oddness is literally reading off binary digits, so an ancient scribe and a modern ALU run the same algorithm.