Boneyard Tools

Twos Complement Calculator

This calculator encodes a signed integer as its twos complement bit pattern and shows that pattern three ways: fixed-width binary, zero-padded hexadecimal, and the plain unsigned value it maps to. It also runs in reverse, reading a bit string of the width you set and returning the signed integer it stands for. Pick 4, 8, 16, or 32 bits, and the range hint tells you the smallest and largest number that fits before you type.

How to find twos complement

  1. Pick a bit width from the Bit width menu (4, 8, 16, or 32 bits); the hint beside it shows the signed range that fits.
  2. In the Encode section, type a signed integer into the Value box.
  3. Read the Binary, Hex, and Unsigned rows, and use the copy button on any row you need.
  4. To decode instead, type a bit pattern of the chosen width into the Binary box in the Decode section.
  5. Read the Signed value the pattern represents below the input.

Examples

Encode -1 in 8 bits

Value -1, width 8 bits
Binary 11111111, hex 0xFF, unsigned 255

Encode the most negative 8-bit value

Value -128, width 8 bits
Binary 10000000, hex 0x80, unsigned 128

Decode a bit pattern back to a signed number

Binary 10000000, width 8 bits
Signed value -128

Frequently asked questions

What is twos complement?

Twos complement is the standard way computers store signed integers. A negative value is the bit pattern you get by adding 2 to the power of the bit width, so -1 in 8 bits is 11111111.

How do I calculate twos complement by hand?

Write the positive value in binary, flip every bit, then add 1. The result is the twos complement form, which this tool computes for you at any width.

Why is the range not symmetric?

An n-bit signed integer runs from minus 2 to the power n minus 1 up to 2 to the power n minus 1, minus 1. For 8 bits that is -128 to 127, so there is one more negative value than positive.

What does the unsigned value mean?

It is the same bit pattern read as a plain unsigned number. For -1 in 8 bits the pattern 11111111 equals 255 when treated as unsigned.

Can it decode binary back to a number?

Yes. Enter a bit pattern of the chosen width in the Decode section and the tool reads the top bit to decide the sign, then returns the signed integer it represents. The pattern must be exactly the width you selected.

Which bit widths can I pick?

The menu offers 4, 8, 16, and 32 bits, which cover the common machine word sizes. The underlying engine accepts any width from 1 to 53 bits through the API, but the calculator itself sticks to the four standard choices.

What happens if my number is out of range?

The tool shows an error instead of a wrong answer. If you enter 200 in 8 bits, for example, it tells you the value does not fit and lists the valid range, since 200 is above the 8-bit maximum of 127.

Can it handle 64-bit integers?

No. Widths are capped at 53 bits because the math runs on JavaScript numbers, which lose exact integer precision above 2 to the power 53. For true 64-bit work use a language with a native 64-bit or BigInt type.

Does my input leave my browser?

No. Every conversion runs locally in your browser with no network call, so the values you type are never uploaded anywhere.

Learn more

Related tools