two's complement calculator

Interactive Two's Complement Calculator

Convert decimal numbers to two's complement binary, or decode two's complement binary back to signed decimal values.

Supported range: 2 to 64 bits.

Enter a whole number (for example: -128, 0, 73).

Ready. Choose a mode, enter a value, and click Calculate.

What is two's complement?

Two's complement is the most common way modern computers represent signed integers (positive and negative whole numbers). It allows hardware to use the same binary addition circuitry for both positive and negative arithmetic, which makes processors simpler and faster.

In a two's complement number system, the leftmost bit is the sign indicator only in interpretation: if that most significant bit (MSB) is 0, the value is non-negative; if it's 1, the value is negative. The exact value depends on the selected bit width, which is why 8-bit and 16-bit values can represent very different ranges.

How this calculator works

Decimal to two's complement binary

  • You choose a bit width, such as 8, 16, or 32 bits.
  • The calculator verifies that your decimal value fits in that width.
  • For positive values, it outputs normal binary padded with leading zeros.
  • For negative values, it computes 2^n + value (where n is bit width), then converts to binary.

Binary to signed decimal

  • You provide a binary string with exactly the selected number of bits.
  • If the MSB is 0, the number is interpreted as positive.
  • If the MSB is 1, the value is interpreted as unsigned - 2^n, producing a negative decimal value.

Why bit width matters

Two's complement is always tied to a fixed number of bits. A binary pattern means different things at different widths:

  • 11111111 as 8-bit = -1
  • 0000000011111111 as 16-bit = 255

For this reason, calculators and programming languages must always know whether you're using 8-bit, 16-bit, 32-bit, or 64-bit integers.

Quick examples

Example 1: Convert -18 to 8-bit two's complement

  • 8-bit range is from -128 to 127, so -18 is valid.
  • Compute 2^8 + (-18) = 256 - 18 = 238.
  • 238 in binary is 11101110.
  • So the 8-bit two's complement result is 11101110.

Example 2: Decode 11101110 (8-bit)

  • MSB is 1, so number is negative.
  • Unsigned value is 238.
  • Signed value is 238 - 256 = -18.

Example 3: Maximum and minimum values

For an n-bit signed two's complement integer:

  • Minimum = -2^(n-1)
  • Maximum = 2^(n-1) - 1

For 8-bit, that's -128 to 127. For 16-bit, -32768 to 32767. For 32-bit, -2147483648 to 2147483647.

Common mistakes and how to avoid them

1) Forgetting to specify width

Always keep track of bit width. The same visible bits can represent different values depending on whether they are treated as 8-bit, 16-bit, or more.

2) Using one's complement by accident

One's complement flips bits but does not add 1. Two's complement requires both operations conceptually for negative conversion from positive magnitude (invert bits, then add one).

3) Ignoring overflow

If a decimal value is outside the valid range for your chosen width, it cannot be represented without overflow. This calculator warns you in that case.

Where two's complement appears in real life

  • Low-level programming in C/C++ and systems languages
  • Microcontroller and embedded firmware development
  • Computer architecture and digital logic coursework
  • Reverse engineering and binary protocol analysis
  • Signal processing pipelines using fixed-width integers

Final takeaway

Two's complement is elegant because it unifies signed arithmetic in binary hardware. Once you understand bit width, signed range, and MSB interpretation, conversions become straightforward. Use the calculator above whenever you need fast, accurate decimal/binary conversion for coding, debugging, or study.

🔗 Related Calculators

🔗 Related Calculators