# What is the 2's complement of x?

Converts a signed whole number to its n-bit two’s complement binary pattern (invert the bits of |x| and add 1), or a two’s complement pattern back to its decimal value, with hex and the steps.

- Page: https://www.acalculator.org/logic/twos-complement-calculator
- JSON spec: https://www.acalculator.org/logic/twos-complement-calculator.json
- Version: e1d06edf4d97

## Default answer

Example with the default inputs (Convert Decimal to binary, Decimal number -28, Number of bits 8): −28 in 8-bit two’s complement is 1110 0100.

## Inputs

| Key | Label | Description |
| --- | --- | --- |
| to | Convert | Decimal to a two’s complement bit pattern, or a bit pattern to decimal. |
| n | Decimal number | A whole number, negative or positive, such as -28. |
| bits | Number of bits | The width of the pattern, from 2 to 64 bits (8, 16, 32 and 64 are common). |
| b | Two’s complement bits | The bit pattern, 1 to 64 binary digits; its length is the width, and its first bit is the sign. |

## Outputs

| Key | Label | Description |
| --- | --- | --- |
| pattern | Two’s complement | The n-bit two’s complement pattern, in groups of 4 bits. |
| value | Decimal value | The signed value of the typed pattern. |
| hex | Hexadecimal | The same bits in hexadecimal. |
| unsigned | Same bits as unsigned | The value of the bits read as an unsigned number, 0 to 2ⁿ − 1. |
| range | Range at this width | The smallest and largest values n bits of two’s complement hold: −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1. |
| steps | Steps | The working, step by step. |
| summary | Answer | The answer in one sentence. |

## Method

For x ≥ 0, the pattern is x in n binary digits. For x < 0, write |x| in n bits, invert every bit and add 1, which equals 2ⁿ + x. A pattern whose first bit is 1 has the value (unsigned value) − 2ⁿ.

## Assumptions

- n bits of two’s complement hold −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1; a number outside that range has no n-bit pattern.
- A typed bit pattern’s width is its number of digits, so its first digit is the sign bit.
- Whole numbers only; arithmetic is exact (no rounding) up to 64 bits.

## Worked examples

1. to = binary, n = -28, bits = 8 gives pattern = 1110 0100, hex = 0xE4, unsigned = 228, range = −128 to 127. Source: Thomas Finley, Two’s Complement, Cornell University CS 104 notes (invert the digits and add one; −28 in 8 bits is 11100100; n bits hold −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1), https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html (retrieved 2026-10-02).
2. to = decimal, b = 11111111111111111111111111111111 gives value = -1, hex = 0xFFFFFFFF, unsigned = 4,294,967,295. Source: Thomas Finley, Two’s Complement, Cornell University CS 104 notes (invert the digits and add one; −28 in 8 bits is 11100100; n bits hold −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1), https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html (retrieved 2026-10-02).
3. to = binary, n = 57, bits = 8 gives pattern = 0011 1001, hex = 0x39. Source: Thomas Finley, Two’s Complement, Cornell University CS 104 notes (invert the digits and add one; −28 in 8 bits is 11100100; n bits hold −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1), https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html (retrieved 2026-10-02): 69 − 12 = 57.
4. to = binary, n = -9,223,372,036,854,775,808, bits = 64 gives hex = 0x8000000000000000, pattern = 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000. Source: Thomas Finley, Two’s Complement, Cornell University CS 104 notes (invert the digits and add one; −28 in 8 bits is 11100100; n bits hold −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1), https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html (retrieved 2026-10-02): −2⁶³ is the smallest 64-bit value.

## FAQ

### How do I find the 2's complement of a negative number?

Write the number without its sign in binary, padded to the width (8 bits, for example). Invert every bit, then add 1. For −28: 28 is 0001 1100; inverted, 1110 0011; plus 1, 1110 0100.

### How do I convert two's complement back to decimal?

Look at the first bit. If it is 0, read the pattern as an ordinary binary number. If it is 1, the number is negative: invert the bits and add 1 to get its size, then put a minus sign in front. 1110 0100 inverts to 0001 1011, plus 1 is 0001 1100 = 28, so it is −28.

### What range can n bits hold in two's complement?

From −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1. 8 bits hold −128 to 127, 16 bits −32,768 to 32,767, and 32 bits −2,147,483,648 to 2,147,483,647. A number outside the range has no pattern at that width.

### What is the 2's complement of a positive number?

A positive number (or 0) is written as plain binary with enough leading zeros to fill the width. 57 in 8 bits is 0011 1001. The first bit is always 0 for a positive number.

### Why do computers use two's complement?

Adding two's complement numbers works with ordinary binary addition, carries and all, so the same circuit adds and subtracts. 69 − 12 is worked out as 69 + (−12) = 57. There is also only one zero.

### What is 1111 1111 in two's complement?

In 8 bits it is −1: invert to 0000 0000, add 1 to get 1. Any width of all ones is −1; 32 ones (0xFFFFFFFF) is −1 in 32 bits. Read as unsigned, the same 8 bits are 255.

## Sources

- Thomas Finley, Two's Complement, Cornell University CS 104 notes (invert the digits and add one; −28 in 8 bits is 11100100; 0xFFFFFFFF is −1; n bits hold −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1; 69 − 12 = 57 by addition). https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html (retrieved 2026-10-02)
