Connect via MCP →

Enter Calculation

Formula

Advertisement

Results

Two's Complement Binary (8-bit)
11111011
two's complement representation
Unsigned value 251
Signed value -5
Bit width 8-bit

What is the Two's Complement Calculator?

Two's complement is the standard way computers store signed integers. This calculator converts any decimal number into its two's complement binary representation for a chosen bit width (4, 8, 16, or 32 bits) and shows both the unsigned pattern value and the signed value that the bits represent.

How to use it

Enter a decimal number — positive or negative — and pick the bit width. The tool reduces the number modulo \(2^n\), producing the exact bit pattern a CPU would store. It also tells you what signed value that same pattern decodes to, so you can verify round-trips.

The formula explained

For an n-bit field, the unsigned representation is $$u = ((x \bmod 2^n) + 2^n) \bmod 2^n.$$ This wraps negative numbers around: in 8 bits, -5 becomes \(256 - 5 = 251\), whose binary is 11111011. To decode a pattern back to a signed value, check the most significant bit (MSB): if it is 1 (i.e. \(u \geq 2^{n-1}\)), the value is \(u - 2^n\); otherwise it is simply \(u\). An equivalent way to negate is to invert all bits and add one: \(\sim x + 1\).

Number line showing unsigned values wrapping into signed positive and negative ranges
Values at or above \(2^{n-1}\) wrap to negative; the upper unsigned half maps to negative signed numbers.

Worked example

Convert −5 to 8-bit two's complement. Step 1: \(2^8 = 256\). Step 2: $$u = ((-5 \bmod 256) + 256) \bmod 256 = 251.$$ Step 3: 251 in binary is 11111011. Since the MSB is 1, the signed value is \(251 - 256 = -5\), confirming the conversion.

Step diagram converting a negative decimal to two's complement binary
Two's complement of a negative value: invert the bits of the magnitude, then add one.

FAQ

What if my number is too big for the bit width? It wraps (overflows) modulo \(2^n\), exactly as hardware would. For example 300 in 8 bits becomes \(300 - 256 = 44\).

Why does the unsigned value differ from the signed value? The same bits can be read two ways. Unsigned reads all bits as positive; signed treats the top bit as a negative weight.

Does this handle 64-bit? This tool supports up to 32-bit common widths to keep results within safe precision.

Last updated: