Twos complement calculator
This twos complement calculator converts signed integers to binary and back. Example: −42 is 1101 0110 in 8 bits.
Source: Hennessy/Patterson, Computer Organization and Design, Kap. „Signed and Unsigned Numbers“ (Zweierkomplement). Updated: .
How it is calculated
Two’s complement is the standard way computers represent negative integers. The highest bit acts as the sign, and addition and subtraction work with the same circuitry – no special cases needed.
How the conversion works
A non-negative number is written in binary as usual and padded with zeros on the left. For a negative number: write the magnitude in binary, flip all bits (ones’ complement) and add 1. In reverse: if the highest bit is set, the value is u − 2ⁿ, where u is the bit string read as an unsigned number. With n bits you can represent −2^(n−1) to 2^(n−1) − 1, so for 8 bits -128 to 127.
Worked example
−42 in 8 bits: the magnitude 42 is 0010 1010. Flipping gives 1101 0101 (ones’ complement), adding 1 gives 1101 0110, in hexadecimal D6. Check: 1101 0110 read as an unsigned number is 214, and 214 − 256 = -42. The bit string 1000 0000 stands for -128, the smallest 8-bit number; +128 no longer fits. In 16 bits, −1 is FFFF in hexadecimal. Read as ones’ complement, 1101 0110 would have the value -41.
- Ones’ complement only flips all bits; it has two zeros (+0 and −0).
- Two’s complement has exactly one zero, but the negative range is one larger.
- Shorter bit strings are padded on the left with 0, never with 1.
Frequently asked questions
How do you convert a negative number to two’s complement?
Write the magnitude in binary, invert all bits and add 1. For −42 in 8 bits: 0010 1010 → 1101 0101 → 1101 0110.
What is the difference between ones’ and two’s complement?
Ones’ complement only flips the bits; two’s complement adds 1 afterwards. That leaves a single zero and lets arithmetic work without special rules.
What range does a byte have in two’s complement?
With 8 bits from -128 to 127, which is 256 different values.
Sources and legal basis
- Hennessy/Patterson, Computer Organization and Design, Kap. „Signed and Unsigned Numbers“ (Zweierkomplement)
- Wikipedia – Zweierkomplement
As of: · Editorial policy
Related tools
- Binary Calculator with StepsBinary calculator online: add, subtract, multiply, divide and combine binary numbers with AND, OR, XOR – result also in decimal, hex and two’s complement.
- Number base converter with stepsConvert between binary, octal, decimal, hex and any base from 2 to 36 – with full working, fractions, repeating digits and two’s complement (8–64 bit).
- Truth Table GeneratorTruth table generator online: enter AND, OR, NOT, XOR, → or ↔ expressions and get the table with every step, plus DNF, CNF and an equivalence check.
- Exponent calculator with stepsCalculate powers, nth roots and logarithms with any base – step-by-step, with negative and fractional exponents, ln, log and scientific notation.
- Logarithm CalculatorLogarithm calculator for any base: log, ln, log10 and log2 with change-of-base working, a check and the two whole numbers the result lies between.
- MB to GB converterConvert MB to GB and back: 1 GB = 1,000 MB, 1 GiB = 1,024 MiB. Also bits, bytes, TB, Mbit and download time – with the working and a conversion table.
- Online CalculatorCalculator online, free and instant: type or tap and see the answer as you go – order of operations, brackets, percent, square root, steps and a history list.
- dBm to watts converterdBm to watts converter and back: power level in dBm, dBW, W, mW and µW plus the RMS voltage across a resistance, with the calculation steps and examples.
- Photon energy calculatorPhoton energy calculator from wavelength, frequency or energy in eV: result in joules and electronvolts, with momentum, calculation steps and exact constants.