Math & Calculators

Two's Complement Calculator

Convert signed decimal integers to two's complement binary and back. See the full step-by-step conversion with a live bit visualization for 8, 16, 32, and 64-bit widths.

About Two's Complement

Two's complement is the most widely used method for representing signed integers in binary. Every modern CPU — from your smartphone to a supercomputer — uses two's complement internally for integer arithmetic. Understanding it is fundamental to low-level programming, embedded systems, and computer architecture.

In an n-bit two's complement system the most significant bit (MSB) carries a negative weight of −2n−1, while all other bits carry their normal positive powers of 2. This single design choice means that addition and subtraction circuits work identically for both positive and negative numbers, which is why hardware designers love it.

Key properties that make two's complement special:

  • There is exactly one representation of zero (unlike sign-magnitude or one's complement, which have +0 and −0).
  • The range for an n-bit value is −2n−1 to +2n−1 − 1 — one more negative number than positive.
  • Adding a positive number and its two's complement always yields zero (with any carry discarded), which is the defining property.
  • Overflow in two's complement arithmetic wraps around predictably — e.g., adding 1 to the maximum value gives the minimum value.
8-bit range: −128 (10000000) to +127 (01111111)
16-bit range: −32 768 to +32 767
32-bit range: −2 147 483 648 to +2 147 483 647

How to Use

  1. Choose a direction. Select Decimal → Binary to encode a signed integer into two's complement, or Binary → Decimal to decode a two's complement bit string back to a signed decimal.
  2. Select bit width (8, 16, 32, or 64-bit) when in Decimal → Binary mode. The bit width determines the valid input range and the length of the output. In Binary → Decimal mode the width is inferred from how many bits you type.
  3. Type your value. Results update live as you type — no button press needed.
    • For Decimal → Binary: enter any whole integer, including negative numbers prefixed with . Values outside the selected bit-width range will show a range error.
    • For Binary → Decimal: type only 0s and 1s. The leftmost bit is treated as the sign bit. Leading zeros matter — 00001101 (8-bit) decodes as +13, while 11110011 decodes as −13.
  4. Read the results. The tool shows the binary, hexadecimal, and octal representations alongside the signed decimal value, plus a colour-coded bit visualisation that highlights the sign bit in red.
  5. Follow the steps. When the result is negative the tool shows the full invert-then-add-1 procedure so you can verify the conversion manually.
  6. Copy any value by clicking the copy icon next to it.

Logic & Algorithm

Converting a negative decimal to two's complement (Decimal → Binary):

  1. Take the absolute value and write it in binary, left-padded with zeros to the target bit width.
  2. Invert every bit (0→1, 1→0). This is the one's complement.
  3. Add 1 to the result (ignoring any carry out of the MSB). The result is the two's complement representation.
Encode −13 in 8-bit two's complement:
① |−13| = 13 → 0000 1101
② Invert → 1111 0010
③ Add 1 → 1111 0011 ✓ (−13 in 8-bit)

Decoding two's complement binary back to decimal (Binary → Decimal):

  1. If the MSB is 0: the number is non-negative. Read the binary value directly as an unsigned integer.
  2. If the MSB is 1: the number is negative. Apply the same procedure in reverse — invert all bits, add 1, read the magnitude, then negate.

Mathematical shortcut: The signed value of any n-bit two's complement string can be computed directly as:

value = −bn−1 × 2n−1 + bn−2 × 2n−2 + … + b1 × 2 + b0
where bn−1 is the sign bit and the rest are data bits with normal positive weights.

Implementation note: In JavaScript, bitwise operators work internally on signed 32-bit integers. This tool handles all widths (8, 16, 32, 64) using pure arithmetic — adding 2n to negative values to get the unsigned representation, and subtracting 2n from values with a set MSB to decode them.

Frequently Asked Questions

Why is there one more negative number than positive in two's complement?
What happens if I overflow — e.g. add 1 to the maximum value?
What is the difference between two's complement and sign-magnitude?
Does this tool support 64-bit values accurately?