acalculator

What is the 2's complement of x?

Type a whole number and the number of bits to get its two's complement pattern, in binary and hex. Or switch to binary to decimal and type a bit pattern to read its signed value. The 2's complement calculator shows each step: write the size of the number in binary, invert the bits, add 1.

Your numbers

Convert
Two’s complement
1110 0100

−28 in 8-bit two’s complement is 1110 0100.

Hexadecimal
0xE4
Same bits as unsigned
228
Range at this width
−128 to 127
Steps
Write 28 in 8 bits: 0001 1100; Invert every bit: 1110 0011; Add 1: 1110 0100
Answer
−28 in 8-bit two’s complement is 1110 0100

Two’s complement: 1110 0100. −28 in 8-bit two’s complement is 1110 0100.

How it is worked out

How to calculate

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.

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.

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ⁿ.

  • 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.

Machine-readable copies: Markdown, JSON.

Worked examples

Each example is checked against the calculator on every build.

  1. Convert Decimal to binary, Decimal number -28, Number of bits 8 gives Two’s complement 1110 0100, Hexadecimal 0xE4, Same bits as unsigned 228, Range at this width −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. Convert Binary to decimal, Two’s complement bits 11111111111111111111111111111111 gives Decimal value -1, Hexadecimal 0xFFFFFFFF, Same bits as 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. Convert Decimal to binary, Decimal number 57, Number of bits 8 gives Two’s complement 0011 1001, Hexadecimal 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. Convert Decimal to binary, Decimal number -9,223,372,036,854,775,808, Number of bits 64 gives Hexadecimal 0x8000000000000000, Two’s complement 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

How it works

An n-bit two's complement pattern stores a whole number x from −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1:

  • x ≥ 0: write x in binary with n digits (leading zeros)
  • x < 0: write |x| in n bits, invert every bit, then add 1; the result is the binary form of 2ⁿ + x
  • reading a pattern: let u be its value as an unsigned binary number. If the first bit is 0, the value is u; if it is 1, the value is u − 2ⁿ
  • hexadecimal: the same unsigned value u in base 16, padded to ⌈n ÷ 4⌉ digits, written with 0x and capital letters
  • range: from −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1

Arithmetic is exact (big integers), so 64-bit values keep every digit.

Rules

  • Decimal to binary: the number is a whole number, typed with an optional minus sign (- or −) and digits; commas are allowed only as thousands separators (1,000, not 1,00). The width is 2 to 64 bits. A number outside the range for the width has no answer.
  • Binary to decimal: 1 to 64 digits, each 0 or 1. Spaces and underscores between digits and a leading 0b (lower case, with no space after it) are ignored; anything else gets a message. The width is the number of digits typed, so the first digit is the sign bit.

Output format. Bit patterns are shown in groups of 4 counted from the right ("1110 0100", "10 1010"). Negative values are written with the minus sign "−". The steps are joined with "; ".

Worked examples by hand

−28 in 8 bits. 28 = 0001 1100; invert: 1110 0011; add 1: 1110 0100 (hex 0xE4; read as unsigned, 228 = 256 − 28). 8 bits hold −128 to 127.

32 ones. The first bit is 1, so the value is u − 2³² = 4,294,967,295 − 4,294,967,296 = −1 (hex 0xFFFFFFFF).

57 in 8 bits. Positive, so plain binary with a leading 0: 0011 1001 (0x39).

−9,223,372,036,854,775,808 in 64 bits (−2⁶³, the smallest 64-bit value): 2⁶⁴ − 2⁶³ = 2⁶³, so the pattern is a 1 and 63 zeros, hex 0x8000000000000000.

Other questions people ask

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.