ECParts Toolkit LogoECParts Toolkit

Two's Complement Calculator

Convert decimal integers to fixed-width two's-complement binary, decode signed binary patterns, negate binary values, analyze signed ranges, and compare sign extension behavior.

DIG-005 focuses on signed integer representation. It does not replace the general hex, decimal, and binary converter, and it does not simulate CPU instructions or floating-point formats.

Engineering tool

Two's Complement Calculator

Convert decimal integers to two's-complement binary, decode signed binary patterns, negate fixed-width values, and analyze sign extension.

Calculator mode

Enter a finite integer, for example -5, 127, or -32768.

Standard widths are 4, 8, 16, 32, and 64 bits.

Result console

Two's Complement Binary
11111011
Decimal Input
-5
Unsigned Encoded Value
251
Hex Representation
0xFB
Sign Bit
1
Overflow Status
Fits

Binary Layout

11111011
Formula: Encoded = x >= 0 ? x : 2^n + x

Formula reference

Two's Complement Rules

Two's complement uses fixed-width binary patterns to represent signed integers.

Signed range = -2^(n-1) to 2^(n-1)-1Positive x: encoded = binary(x)Negative x: encoded = 2^n + xDecode negative pattern: value = unsigned - 2^nNegation: (~x + 1) mod 2^nSign extension copies the sign bit

Variable definitions

n
bit width
Sign bit
most significant bit
Unsigned value
raw binary pattern value
Signed value
two's-complement interpretation
Overflow
value outside signed representable range
BigInt
precise integer arithmetic for 32-bit and 64-bit values

Worked Examples

+5, 8-bit

00000101

-5, 8-bit

11111011

-1, 8-bit

11111111

+127, 8-bit

01111111

-128, 8-bit

10000000

+128, 8-bit signed

Out of range

Decode 11111111

8-bit signed value is -1

Decode 10000000

8-bit signed value is -128

Negate 00000101

11111011, so +5 becomes -5

Negate 10000000

Minimum-negative value wraps and overflows

Sign extend 11111011

8-bit to 16-bit becomes 1111111111111011

Zero extend 11111011

8-bit -5 becomes 16-bit signed +251

16-bit -32768

1000000000000000

32-bit -1

11111111111111111111111111111111

64-bit -1

64 one bits, handled with BigInt

Engineering Notes

  • Two's complement is the dominant signed integer representation in modern digital systems.
  • The most significant bit is part of the numeric weight system; it is not merely a separate minus sign.
  • The same bit pattern can have different signed and unsigned interpretations.
  • The n-bit signed range is asymmetric: -2^(n-1) to 2^(n-1)-1.
  • The minimum negative value has no positive counterpart in the same signed width.
  • Two's-complement negation is invert bits plus add 1 inside a fixed bit width.
  • Sign extension copies the sign bit; zero extension can change negative signed values.
  • This calculator uses BigInt to avoid 32-bit coercion and precision loss.

Common Mistakes

  • Treating the most significant bit as a separate negative sign.
  • Reading signed binary as unsigned.
  • Forgetting that negative values require a fixed bit width.
  • Inverting bits but forgetting to add 1.
  • Zero extending a negative signed value.
  • Ignoring minimum-negative negation overflow.
  • Assuming 8-bit signed range is -127 to +127.
  • Interpreting 0xFF as only 255 when it can mean -1 in signed 8-bit.
  • Using JavaScript 32-bit bitwise operators for 64-bit values.
  • Ignoring bit width when comparing results.

Support reference

FAQ

What is two's complement?

Two's complement is the most common fixed-width signed integer representation used in modern digital systems.

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

For an n-bit negative value x, encode it as 2^n + x, then write the result as an n-bit binary pattern.

How do I convert two's complement to decimal?

If the sign bit is 0, read the unsigned value. If the sign bit is 1, subtract 2^n from the unsigned value.

Why do you invert bits and add 1?

Invert bits and add 1 is a practical fixed-width method to calculate the two's-complement negation of a binary pattern.

What is the signed range of an 8-bit integer?

An 8-bit two's-complement integer ranges from -128 to +127.

Why is the 8-bit range -128 to 127?

There are 256 total bit patterns. Half represent negative values, one represents zero, and the remaining patterns represent positive values.

What is sign extension?

Sign extension increases bit width by copying the sign bit into the new higher-order bits.

What is the difference between zero extension and sign extension?

Zero extension pads with 0. Sign extension pads with the sign bit. Zero extension changes negative signed values.

What happens when the minimum negative value is negated?

The minimum negative value wraps to itself in fixed-width arithmetic and raises signed overflow.

Is 11111111 equal to -1 or 255?

Both interpretations are possible. As unsigned 8-bit it is 255; as signed two's complement it is -1.

Why does bit width matter in two's complement?

Bit width determines the signed range, the sign bit position, and whether a value can be represented without overflow.

Why should BigInt be used for 64-bit values?

JavaScript Number and bitwise operators can lose precision or coerce to 32-bit behavior. BigInt preserves fixed-width integer arithmetic.

Binary Arithmetic Calculator

Available

Calculate fixed-width binary arithmetic, carry, borrow, and overflow.

Open calculator

Hex Decimal Binary Converter

Available

Convert basic number bases without signed fixed-width interpretation.

Open calculator

Binary Bitwise Calculator

Coming_Soon

Planned calculator for masks, shifts, AND, OR, XOR, and NOT.

Gray Code Converter

Coming_Soon

Planned calculator for binary and Gray code conversion.

Planned Guide

Two's Complement Explained

Planned Guide

Signed vs Unsigned Binary

Planned Guide

Bit Width and Integer Range

Planned Guide

Binary Overflow Explained

Planned Guide

Sign Extension in Digital Systems