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
- 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.
- In the Encode section, type a signed integer into the Value box.
- Read the Binary, Hex, and Unsigned rows, and use the copy button on any row you need.
- To decode instead, type a bit pattern of the chosen width into the Binary box in the Decode section.
- 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
- How twos complement stores negative numbers
Why hardware picks twos complement over sign-and-magnitude, the invert-and-add-one trick, and how fixed-width wraparound causes overflow.
Related tools
Number Base Converter
Convert numbers between any base from 2 to 36, including binary, octal, decimal and hex. Handles huge numbers exactly with arbitrary precision. Free, private.
UUID Generator
Generate random version 4 UUIDs online. Create one or thousands at once, with uppercase or no-hyphen options, then copy them. Free and private.
.env to JSON
Convert a .env file to JSON, or JSON back to .env. Parses KEY=value lines, comments, quotes and export. Runs entirely in your browser.
ASCII Table
Full ASCII table for all 128 codes with decimal, hex, octal and binary values, character names and descriptions. Search by code, hex or character.
Aspect Ratio Box Generator
Generate CSS for a responsive aspect-ratio container. Use the modern aspect-ratio property or the padding-top fallback, then copy the ready code.
Aspect Ratio Calculator
Calculate aspect ratios fast. Enter a ratio like 16:9 and one dimension to get the other, or enter width and height to simplify the ratio.