Math

Two's Complement Calculator

Encode a signed decimal integer or decode a binary, hexadecimal, or octal two's-complement bit pattern.

Free to use — no sign-up or login.

Conversion direction

The selected format controls parsing; bare values are never auto-detected.

Enter a whole base-10 integer. Fractions are rejected.

Presets preserve leading zeros. Custom widths must be whole numbers from 2 to 256.

Representations

Padded binary

11111011

Grouped binary

1111 1011

Hexadecimal

FB

Octal

373

Signed decimal

-5

Unsigned decimal

251

Sign bit

1

Calculation steps

  1. 1.Range: -2^(7) to 2^(7) - 1 = -128 to 127.
  2. 2.Modulo: -5 mod 2^8 (256) = 251.
  3. 3.Invert 5: 00000101 becomes 11111010.
  4. 4.Add 1: 11111010 + 1 = 11111011.

Two's complement is a fixed-width way to represent signed integers. The same bits can be read as an unsigned value or as a signed value; the selected width tells you which bit is the sign bit and what range is available.

Use Encode when you know the signed decimal integer and need its bit pattern. Use Decode when you already have binary, hexadecimal, or octal digits and need the signed decimal value they represent at a specific width.

What is two's complement?

At width w, an encoded integer is its value modulo 2^w. Non-negative values use ordinary binary with leading zeros. For a negative value, you can encode the magnitude in w bits, invert every bit, and add 1; this gives the same modulo 2^w pattern. When decoding, a most-significant bit of 1 means the signed value is the unsigned value minus 2^w. A leading 0 means the signed and unsigned values are the same.

Bit widths, signed ranges, and overflow

A w-bit two's-complement integer ranges from −2^(w−1) to 2^(w−1)−1, so 8 bits cover −128 through 127. Width is part of the data: 11111011 is −5 at 8 bits but 251 when zero-extended to 16 bits. To widen a signed value without changing it, repeat its sign bit on the left; this is sign extension. This calculator reports overflow instead of silently wrapping an out-of-range decimal value or accepting a raw pattern that does not fit.

One's complement vs two's complement

One's complement negates a pattern by flipping every bit. It has both positive and negative zero and an equal-sized positive and negative range. Two's complement flips the bits and adds 1, giving one zero and one extra negative value. That add-one step is why an 8-bit −5 is 11111011 rather than the one's-complement pattern 11111010.

Decimal to two's complement: −5 at 8 bits

Encode the signed decimal integer −5 in an 8-bit two's-complement field.

  1. Check the 8-bit signed range: −128 to 127, so −5 fits.

  2. Write the magnitude 5 as 00000101, then invert every bit to get 11111010.

  3. Add 1: 11111010 + 1 = 11111011. The same pattern has unsigned value 251.

  4. Group or convert the pattern: binary 11111011, hexadecimal FB, octal 373, signed decimal −5, and unsigned decimal 251.

Decode 11111011 at 8 bits to signed decimal

Interpret the raw binary pattern 11111011 as an 8-bit two's-complement integer.

  1. The pattern has eight bits and its sign bit is 1, so it represents a negative signed value.

  2. Read the same bits as unsigned binary: 11111011 = 251.

  3. Subtract the 8-bit modulus: 251 − 2^8 = 251 − 256 = −5.

  4. The equivalent raw patterns are hexadecimal FB and octal 373; all decode to signed decimal −5 at 8 bits.

Frequently asked questions

How do I convert a decimal integer to two's complement?

Choose Encode, enter the signed whole number, and select a bit width. If the value is in range, the calculator reduces it modulo 2^w and shows the padded binary, hexadecimal, octal, signed, and unsigned forms.

How do I convert binary or hexadecimal two's complement to signed decimal?

Choose Decode, select Binary or Hexadecimal (or Octal), enter the raw pattern, and choose its intended width. A sign bit of 0 keeps the unsigned value; a sign bit of 1 means subtract 2^w to get the signed decimal value.

Which bit width should I choose?

Match the width of the source field, register, instruction, or file format. For a new decimal value, choose a width whose range contains it: −2^(w−1) through 2^(w−1)−1. Do not drop leading bits, because changing the width can change the sign.

Why doesn't the calculator accept fractions?

This tool represents fixed-width signed integers. A fraction needs an additional fixed-point scale or a floating-point format, neither of which is specified by an integer two's-complement bit pattern.

What happens when a value is outside the signed range?

That is overflow for the selected width. The calculator rejects the value rather than wrapping it, so you can choose a wider field or correct the input. For example, 128 does not fit in the 8-bit signed range of −128 to 127.

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

One's complement flips every bit. Two's complement flips every bit and adds 1. As a signed representation, one's complement has two zeros; two's complement has one zero and uses the extra pattern for the most negative value.

References