OtherCalculators

Two's Complement Calculator

Enter a negative or positive whole number to see its two's complement bit pattern at 8, 16, 32 or 64 bits, with the invert-and-add-one working laid out.

Switch to Bits β†’ number to decode a pattern, and compare how the same bits read as unsigned, one's complement and sign-magnitude. Values that do not fit are flagged as overflow.

Last updated: Β· Reviewed by Shakeel Alvi, OtherCalculators

Calculator mode

Number to bits

Direction

Enter a whole number, negative or positive.

Width
Encoding

8-bit two's complement holds -128 to 127.

-5 in 8-bit two's complement

1111 1011

Hex
0xFB
As unsigned
251

The 8 bits

  • Sign
  • Bit

In two's complement the top bit is worth βˆ’2⁷ (-128) and every other bit keeps its usual positive value.

Invert and add one

Two's complement working: magnitude, inverted bits, plus one
|-5| in binary0000 0101
Flip every bit1111 1010
Add 11111 1011

Check: the 8-bit pattern read as unsigned is 251, and 251 βˆ’ 2⁸ = -5.

The same 8 bits, read four ways

What this bit pattern means in each encoding
EncodingValue
Unsigned251
Two's complement-5
One's complement-4
Sign-magnitude-123

A pattern does not carry its encoding: the program reading it decides which of these it means.

Exact at any size

Whole numbers are worked digit by digit, so 64-bit values and beyond never round off after 15 or 16 digits. Floating-point mode prints the exact value the bits store.

Runs in your browser

What you enter is worked out on this page and is not sent anywhere. Copy the address to share a conversion.

Two's ComplementSigned Binary8 / 16 / 32 / 64-bitOverflow CheckOne's Complement

Quick answer

To write a negative number in two's complement, write its size in binary at the full width, flip every bit, then add 1. For βˆ’5 in 8 bits: 00000101 flips to 11111010, and adding 1 gives 11111011. An 8-bit two's complement number holds βˆ’128 to 127, and a leading 1 always means the value is negative.
Guide

How the two's complement calculator works

Choose a width and an encoding, then type a decimal number. The result card shows the bit pattern in groups of four with its hex form, and the bit grid marks the sign bit. For a negative number in two's complement, the working shows the size in binary, every bit flipped, and the 1 added, with a check that reading the pattern as unsigned and subtracting 2 to the power of the width gives your number back.

The β€œread four ways” table shows what the same bits mean as unsigned, two's complement, one's complement and sign-magnitude β€” a pattern never records its own encoding. To add or subtract signed values, use the binary calculator.

How the maths works

The two's complement formula

Pattern of βˆ’x = 2ⁿ βˆ’ x (n = width in bits)
Value of a pattern = unsigned value βˆ’ 2ⁿ, if the top bit is 1
Top bit weight = βˆ’2ⁿ⁻¹

Example: βˆ’5 in 8 bits is 2⁸ βˆ’ 5 = 251 = 11111011. Reading it back: βˆ’128 + 64 + 32 + 16 + 8 + 2 + 1 = βˆ’5.

Worked example

Worked examples

Example 1 β€” encoding βˆ’100 in 8 bits

Number:
βˆ’100
Width:
8 bits
  1. 100 in binary: 01100100
  2. Invert every bit: 10011011
  3. Add 1: 10011100

Two's complement: 10011100 (0x9C)

Check: 10011100 is 156 unsigned, and 156 βˆ’ 256 = βˆ’100.

Example 2 β€” decoding a 16-bit pattern

Pattern:
1111 1111 1000 0000
  1. Top bit is 1: negative
  2. Read as unsigned: 65,408
  3. Subtract 2¹⁢: 65,408 βˆ’ 65,536

Value: βˆ’128

It is the 8-bit pattern 10000000 sign-extended to 16 bits by copying the 1 into the new top byte.

Reference data

Integer ranges by width

The smallest and largest value of two's complement and unsigned integers at 8, 16, 32 and 64 bits
WidthTwo's complement (signed)Unsigned
8-bitβˆ’128 to 1270 to 255
16-bitβˆ’32,768 to 32,7670 to 65,535
32-bitβˆ’2,147,483,648 to 2,147,483,6470 to 4,294,967,295
64-bitβˆ’9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070 to 18,446,744,073,709,551,615
Source: cppreference.com β€” Fundamental types: signed integer representation and ranges β€” ranges computed by the calculator's engine and matching the published table

Key facts

  • In n-bit two's complement, a negative number βˆ’x is stored as the unsigned value 2ⁿ βˆ’ x.
  • An 8-bit two's complement integer holds βˆ’128 to 127, and a 32-bit one holds βˆ’2,147,483,648 to 2,147,483,647.
  • In two's complement, βˆ’1 is all ones at every width, and the most negative number is a 1 followed by all zeros.
  • Since C++20, two's complement is the only signed integer representation the C++ standard allows.
When you need this

Why computers use two's complement

With two's complement, the circuit that adds unsigned numbers also adds signed ones: 11111011 (βˆ’5) plus 00000011 (3) is 11111110, which is βˆ’2, with no special case for the sign. There is also only one zero, unlike one's complement and sign-magnitude. That is why every mainstream processor uses it, and why C++20 made it the only representation the language allows (source: cppreference). You need this when reading a memory or register dump, decoding a sensor value that arrives as raw bytes, debugging an integer overflow, or answering a computer architecture exam question.

Tips

Common two's complement mistakes

  • Forgetting the width. βˆ’5 is 11111011 in 8 bits but 1111111111111011 in 16. Without a width a negative pattern is meaningless.
  • Inverting without adding 1. That gives one's complement: 11111010 is βˆ’5 only in one's complement; read as two's complement it is βˆ’6.
  • Padding with zeros. Widening a negative value means copying its sign bit (sign extension), not adding zeros on the left.
  • Negating the minimum. βˆ’(βˆ’128) does not fit in 8 bits; inverting and adding 1 to 10000000 gives 10000000 again.
FAQ

Frequently Asked Questions

What is βˆ’1 in two's complement?

βˆ’1 is all ones at every width: 11111111 in 8 bits, 16 ones in 16 bits, and so on. Invert 00000001 to get 11111110, then add 1 to get 11111111. Read as unsigned, the same pattern is 255.

How do you find the two's complement of a negative number?

Write the number's size in binary at the full width, flip every bit, then add 1. For βˆ’12 in 8 bits: 12 is 00001100, flipped it is 11110011, and adding 1 gives 11110100. Another way is to subtract the size from 2 to the power of the width: 256 βˆ’ 12 = 244, which is 11110100.

How do you convert a two's complement number back to decimal?

If the top bit is 0, read it as ordinary binary. If the top bit is 1, the number is negative: read the bits as unsigned and subtract 2 to the power of the width. 11110100 is 244 unsigned, and 244 βˆ’ 256 = βˆ’12.

Why can 8 bits hold βˆ’128 but only +127?

There are 256 patterns. Zero uses one, leaving 255 to split between negatives and positives, so one side gets an extra value. Two's complement gives it to the negatives: 10000000 is βˆ’128, and its positive counterpart +128 would need a ninth bit.

What happens when a two's complement number overflows?

Adding 1 to the largest value wraps to the smallest: in 8 bits, 127 + 1 gives 10000000, which reads as βˆ’128. The calculator reports a value outside the range as an overflow, shows the range, and shows the pattern a register would wrap it to.

What is the difference between one's and two's complement?

One's complement only flips the bits, so βˆ’5 is 11111010 and there are two zeros, 00000000 and 11111111. Two's complement flips the bits and adds 1, so βˆ’5 is 11111011, there is one zero, and ordinary binary addition gives the right answer for negative numbers too.

Keep going

Notes & Assumptions

  • Inputs are whole numbers, worked exactly with arbitrary-precision integers at 8, 16, 32 or 64 bits.
  • A value outside the range is reported as overflow rather than wrapped silently; for unsigned and two's complement the wrapped pattern is shown as well.
  • Fractional and floating-point values are not encoded here; the IEEE 754 converter covers those.

This tool is for education and general calculation. Check how a particular language or processor handles overflow against its documentation.