🧮
🧮 Math & statistics

Two’s Complement Calculator

Results update as you type.

Your numbers

Understanding the two's complement calculator — concept and principle

Two's complement is the way almost every modern computer stores signed integers. This calculator shows the bit pattern and hexadecimal form of a decimal integer in 8, 16 or 32 bits, together with the range of values that width can hold. It is useful for programming, debugging binary data, reading memory dumps and studying computer architecture.

Two’s complement of −42 in 8 bitsBits 11010110 with place values −128, 64, 32, 16, 8, 4, 2, 1 add up to −42. Flipping 00101010 and adding 1 gives the same pattern, D6 in hex.−42 in 8 bits: the top bit is worth −1281−12816403211608141201−128 + 64 + 16 + 4 + 2 = −42Shortcut: flip every bit, then add 1420010 1010flip1101 0101+ 11101 0110= D6 (hex)Also: 256 − 42 = 214 = 110101108 bits hold −128 … 127
The leftmost bit counts as −128, so a 1 there makes the number negative.

How it is calculated

An equivalent hand method for negatives: write |n| in binary, invert every bit, then add 1. Values outside the range are reported as "out of range".

The principle

A b-bit register counts modulo 2^b: after the largest pattern (all ones) it wraps around to zero. In that system, 2^b + n behaves exactly like n, because adding 2^b changes nothing modulo 2^b. So if −42 is stored as 256 − 42, ordinary unsigned addition gives correct signed results; for example 42 + (256 − 42) = 256, which wraps to 0. Hardware therefore needs no separate subtraction circuit, and there is only one representation of zero. The leftmost bit acts as a sign bit: it is 1 for every negative value, because it carries the weight −2^(b−1).

Worked example

  1. Inputs: decimal −42, 8 bits.
  2. Range check: −128 ≤ −42 ≤ 127, so the value fits.
  3. 2⁸ + (−42) = 256 − 42 = 214.
  4. 214 in binary: 11010110.
  5. Check with invert-and-add-one: 42 = 00101010 → 11010101 → + 1 = 11010110.
  6. Hex: 1101 = D and 0110 = 6, so D6. Range: −128 … 127.

How to read the result

A leading 1 means the value is negative. To decode a negative pattern, read it as unsigned and subtract 2^b: 214 − 256 = −42. The same −42 in 16 bits is FFD6, because the sign bit is copied into the new high bits (sign extension). The range is asymmetric: there is one more negative value than positive, so −128 has no positive partner in 8 bits.

Common mistakes

Keep in mind: The tool covers integers only. Programming languages differ in how they handle overflow and in the default width of integer types, so check your language's rules.

Frequently asked questions

Why not just use a sign bit plus magnitude?

Sign-magnitude has two zeros (+0 and −0) and needs extra logic for addition, which two's complement avoids.

What is the two's complement of the most negative number?

It is itself. In 8 bits, −(−128) cannot be represented and wraps back to −128.

Does two's complement apply to floating-point numbers?

No. IEEE 754 floating-point numbers use a separate sign bit with an exponent and fraction.

Sources: D. A. Patterson and J. L. Hennessy, Computer Organization and Design, Chapter 2 (signed and unsigned numbers) · D. E. Knuth, The Art of Computer Programming, Vol. 2: Seminumerical Algorithms, Section 4.1

Checked 8 Nov 2026. Standard formulas, computed in your browser with double-precision numbers.

🧮 All math & statistics tools →🌍 Calculators by country →