Two’s Complement Calculator

Get the binary form of negatives.

How it is calculated

Given a whole number n and a width of w bits, the tool first checks that n fits the range −2w−1 ≤ n ≤ 2w−1 − 1. For a negative n it computes n + 2w, which is exactly the invert-and-add-one rule. Positive numbers and zero are written as ordinary binary. The result is padded with leading zeros to exactly w digits. The width must be a whole number from 2 to 32. Anything else gives no result: a fractional number, a width of 1 or 33, or a value outside the range for that width (for example 128 or −129 with 8 bits). The output is a bit string, not a decimal value, and it has no unit.

Worked example

Write −42 in 8 bits. The allowed range for 8 bits is −128 to 127, so −42 fits. Start from +42 = 00101010. Invert every bit to get 11010101, then add 1 to get 11010110. The same answer comes from the formula: −42 + 28 = 256 − 42 = 214, and 214 in binary is 11010110. The tool prints 11010110. The leading 1 marks the number as negative.

Try this example →

Common mistakes

  • Choosing a width that is too small. With 8 bits the range ends at 127 and −128, so 128 or −129 returns nothing; widen to 16 bits for those.
  • Reading the result as an unsigned number. 11010110 is −42 in 8-bit two's complement but 214 if you treat it as unsigned.
  • Forgetting the width. The same −1 is 11 in 2 bits, 1111 in 4 bits and 32 ones in 32 bits, so the bit count must match your system.
  • Entering a binary or hex string as the number. The box expects an ordinary decimal integer, such as −42.
  • Expecting a symmetric range. Two's complement has one more negative value than positive: −128 exists in 8 bits (10000000), but +128 does not.

About this calculator

Two's complement is the standard method computers use to represent signed, positive and negative, integers in binary: to find the two's complement of a number, you invert every bit of its binary representation, called the one's complement, and then add 1. This scheme lets a single set of binary addition circuitry handle both addition and subtraction without any special-case logic, since subtracting a number is the same as adding its two's complement.

Virtually every modern CPU, from microcontrollers to desktop processors, uses two's complement internally to store signed integers because it has exactly one representation for zero, unlike sign-magnitude or one's-complement encoding which have both a “positive zero” and “negative zero,” and because overflow behavior is well-defined and consistent, making it foundational knowledge for computer science students, embedded systems engineers, and anyone debugging low-level binary or hexadecimal data.

Was this helpful?

You can pick up to 3 reactions.

Comments

  • Be the first to comment.

Popular calculators

All Calculators