Two's Complement Calculator
Convert a signed decimal value into fixed-width two's complement binary, or decode a two's complement bit pattern back into decimal. Exact at every supported width, including 64-bit, using BigInt arithmetic.
What Is Two's Complement?
Two's complement is the standard way computers represent signed (positive and negative) integers in a fixed number of bits. Positive numbers are written as ordinary binary; negative numbers are represented as the value's complement modulo 2N, where N is the bit width you choose.
Because bit width is fixed in advance, the same N bits can represent a range of both positive and negative integers: from -2(N-1) up to 2(N-1) - 1. Choosing 8-bit, 16-bit, 32-bit, or 64-bit changes that range, so the same decimal value can produce a different bit pattern depending on width.
Use Decimal → Two's Complement above to convert a signed decimal number into its binary form at a chosen bit width, or Two's Complement → Decimal to interpret an existing bit pattern as a signed number. If you only need an ordinary unsigned decimal-to-binary conversion — no sign bit, no fixed width — the Decimal to Binary Converter is the more direct tool; for a plain hexadecimal value's unsigned base-10 equivalent, use the Hex to Decimal Converter instead.
For the full explanation behind these conversions — why bit width changes the result, how the range works, and how two's complement compares to one's complement — see Two's Complement Explained.
How Two's Complement Is Calculated
For a negative value, at a fixed bit width N:
- Write the absolute value in N-bit binary.
- Invert every bit (one's complement).
- Add 1 to the result.
Worked example: -5 in 8-bit two's complement
Decimal: -5 Step 1: Write +5 in 8-bit binary 00000101 Step 2: Invert all bits (one's complement) 11111010 Step 3: Add 1 11111010 + 1 ---------- 11111011 Result: -5 in 8-bit two's complement = 11111011
The reverse works the same way: given a bit pattern whose most significant bit (MSB) is 1, invert the bits and add 1 to find the magnitude, then apply a negative sign. The MSB is the sign bit: 0 means positive, 1 means negative. For arithmetic on unsigned binary values, see the Binary Calculator; for AND/OR/XOR/shift operations, see Bitwise Operations.
Signed Range by Bit Width
For N-bit two's complement, the valid signed range is -2(N-1) to 2(N-1) - 1:
| Bit Width | Minimum | Maximum | Total Values |
|---|---|---|---|
| 4-bit | -8 | 7 | 16 |
| 8-bit | -128 | 127 | 256 |
| 16-bit | -32,768 | 32,767 | 65,536 |
| 32-bit | -2,147,483,648 | 2,147,483,647 | 4,294,967,296 |
| 64-bit | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | 18,446,744,073,709,551,616 |
64-bit values exceed what JavaScript's Number type can represent exactly, so this calculator computes them with BigInt rather than Number or 32-bit bitwise operators.
Why Computers Use Two's Complement
Single addition circuit
Subtraction becomes addition of a negative number, so hardware needs only one arithmetic circuit.
One representation of zero
Unlike one's complement, which has both +0 and -0, two's complement has a single zero bit pattern.
No special-cased sign logic
Addition and subtraction work the same way regardless of the operands' signs.
Universal in modern CPUs
Virtually all processors use two's complement for signed integer types.
8-bit Two's Complement Examples
| Decimal | 8-bit Two's Complement |
|---|---|
| 0 | 00000000 |
| 1 | 00000001 |
| 2 | 00000010 |
| 5 | 00000101 |
| 127 | 01111111 |
| -1 | 11111111 |
| -2 | 11111110 |
| -5 | 11111011 |
| -128 | 10000000 |
128 and -129 are both invalid in 8-bit signed two's complement, since the valid range is -128 to 127.
Frequently Asked Questions
How do I convert a negative decimal number to two's complement?
Choose a bit width, write the absolute value in binary, flip all the bits, then add 1. For example, -5 in 8-bit two's complement is 11111011. The "Decimal → Two's Complement" mode above does this directly.
How do I read a two's complement binary value back into decimal?
Check the leftmost bit (MSB). If it is 0, read the value as ordinary unsigned binary. If it is 1, the number is negative — flip all the bits, add 1, and negate the result. The "Two's Complement → Decimal" mode does this automatically.
Why is the range asymmetric, e.g. -128 to 127 for 8-bit?
Zero itself uses one positive bit pattern (00000000), leaving one fewer pattern available for positive numbers than negative ones. That is why the minimum is -2^(n-1) and the maximum is 2^(n-1) - 1.
What happens if I enter a binary value shorter than the selected bit width?
It is padded with leading zeros to match the selected width, which preserves its value as a positive number. Padding never changes a value’s sign, so a negative number must be entered at its full bit width, including its leading 1s.
What's the difference between one's complement and two's complement?
One's complement only flips the bits. Two's complement flips the bits and adds 1. Two's complement is what real hardware uses because it produces a single representation of zero and lets addition and subtraction share one circuit.
Can this calculator handle 64-bit values without losing precision?
Yes. All calculations use exact BigInt arithmetic rather than JavaScript's 32-bit bitwise operators or the floating-point Number type, so 64-bit boundary values like -9223372036854775808 and 9223372036854775807 convert exactly.
Related Tools
Decimal to Binary Converter
Ordinary unsigned decimal-to-binary conversion, no sign bit or fixed width.
Binary to Decimal Converter
Convert plain unsigned binary to decimal.
Binary Calculator
Add, subtract, multiply, and divide unsigned binary values.
Bitwise Operations
AND, OR, XOR, NOT, and shift operations on binary values.
Related Tools
You might also find these tools useful
Base Converter
Convert whole numbers between binary, octal, decimal, hex, and custom bases 2-36 in one hub.
Binary Converter
Convert text, UTF-8 bytes, and binary values without focusing only on readable messages.
Binary Calculator
Add, subtract, multiply, and divide binary numbers for math and computer science exercises.
Binary to Decimal
Turn base-2 numbers into decimal values for programming, networking, and study notes.
Binary to HEX
Compress long binary strings into hexadecimal groups for code, memory, and color checks.
Decimal to Binary
Convert everyday base-10 numbers into binary with clear fixed-width output.