Convert decimal integers to two's complement binary representation and back. Adjustable bit length (4–32 bits), real‑time bit visualization, overflow detection, and educational deep dives.
Two's complement is the de facto standard for representing signed integers in modern computing (ARM, x86, RISC‑V). Unlike sign‑magnitude or one's complement, it provides a single representation for zero, simplifies arithmetic logic unit (ALU) design, and enables seamless addition/subtraction using the same hardware.
For an n‑bit system: a negative number -x is represented as 2ⁿ − x. The most significant bit (MSB) carries the sign: 0 = non‑negative, 1 = negative. The decimal value is computed as:
Value = −bn-1·2ⁿ⁻¹ + Σi=0n-2 bi·2ⁱ
This elegant weighting makes binary addition work naturally without special handling for signed numbers. Overflow occurs when the result falls outside the interval [-2ⁿ⁻¹, 2ⁿ⁻¹ − 1].
Two's complement was pioneered in early computer design by John von Neumann and became ubiquitous due to its mathematical consistency. Today, every mainstream programming language (C, C++, Java, Python, Rust) relies on two's complement for signed integer semantics (C++20 mandated it). Digital circuit designers use it for efficient subtraction via add with complement.
Real‑world applications: Digital signal processing (DSP), fixed‑point arithmetic, embedded sensor calibration, file formats (WAV, binary protocols), and error detection. Understanding two's complement is crucial for debugging overflow bugs, bitwise operations, and low‑level optimizations.
In an 8‑bit embedded system measuring temperature (-40°C to +85°C), the ADC delivers two's complement values. A naive addition of two positive readings may overflow if the result exceeds +127. The calculator's overflow detection helps engineers emulate the behavior and anticipate saturation logic in safety‑critical systems.