Math

Two's Complement Calculator

Convert between decimal and two's complement binary representation

Input mode

You can enter a number between -128 and 127.

Two's complement
1101 0110
Decimal
-42
Binary (8-bit)
1101 0110
Hexadecimal
0xD6
Sign bit
1 (negative)

Conversion steps

1. Start with |-42| = 42

2. Binary of 42: 0010 1010

3. Invert all bits: 1101 0101

4. Add 1: 1101 0110

What is two's complement?

Two's complement is the standard method computers use to represent signed integers (positive and negative whole numbers) in binary. Unlike everyday decimal notation where we simply place a minus sign before negative numbers, computers work exclusively with binary digits (0s and 1s), requiring a different approach to distinguish positive from negative values.

In two's complement representation, the leftmost bit (most significant bit) serves as the sign bit: 0 indicates a non-negative number, and 1 indicates a negative number. However, the remaining bits don't simply represent the magnitude—the entire number is encoded in a way that makes arithmetic operations work seamlessly.

Two's complement has become the universal standard for signed integer representation in virtually all modern computing systems because of its elegant mathematical properties. Addition, subtraction, and comparison operations work identically for both positive and negative numbers, eliminating the need for separate hardware circuits to handle signed arithmetic.

How two's complement works

Representing positive numbers

Positive numbers in two's complement are straightforward: simply convert the decimal number to binary and pad with leading zeros to fill the bit width.

For example, the number 42 in 8-bit two's complement:

4210=00101010242_{10} = 00101010_2

The sign bit (leftmost) is 0, indicating a positive number. The remaining 7 bits represent the value 42 in standard binary.

Representing negative numbers

Negative numbers require a specific conversion process:

  1. Start with the absolute value and convert it to binary
  2. Invert all the bits (change 0s to 1s and 1s to 0s)
  3. Add 1 to the result

For example, to represent -42 in 8-bit two's complement:

42=4210=001010102Invert bits:110101012Add 1:110101012+1=110101102\begin{aligned} |{-42}| &= 42_{10} = 00101010_2 \\ \text{Invert bits} &: 11010101_2 \\ \text{Add 1} &: 11010101_2 + 1 = 11010110_2 \end{aligned}

The result 11010110 is the 8-bit two's complement representation of -42. Notice the sign bit is 1, indicating a negative value.

Converting two's complement to decimal

To convert a two's complement binary number back to decimal:

For positive numbers (sign bit is 0): Simply convert the binary to decimal normally.

For negative numbers (sign bit is 1):

  1. Invert all the bits
  2. Add 1
  3. Convert to decimal and negate

Alternatively, you can use the formula:

value=unsigned value2n\text{value} = \text{unsigned value} - 2^n

Where n is the bit width. For example, 11010110 in 8-bit:

unsigned=214,value=214256=42\text{unsigned} = 214, \quad \text{value} = 214 - 256 = -42

Value ranges for different bit widths

The range of representable values depends on the bit width. With n bits, you can represent:

Bit widthMinimum valueMaximum valueTotal values
8-bit-128127256
16-bit-32,76832,76765,536
32-bit-2,147,483,6482,147,483,6474,294,967,296

The general formula for an n-bit two's complement representation:

Range=[2n1,2n11]\text{Range} = [-2^{n-1}, 2^{n-1} - 1]

Notice there's one more negative value than positive values. This asymmetry occurs because zero is considered non-negative (its sign bit is 0), leaving one extra pattern for a negative number.

Why two's complement is used

Simplified arithmetic

The primary advantage of two's complement is that addition and subtraction work identically for positive and negative numbers. The same hardware adder circuit handles all cases, and subtraction is simply addition of the negated value.

For example, to compute 5 - 3 using 8-bit two's complement:

5=0000010123=1111110125+(3)=00000101+11111101=000000102=2\begin{aligned} 5 &= 00000101_2 \\ -3 &= 11111101_2 \\ 5 + (-3) &= 00000101 + 11111101 = 00000010_2 = 2 \end{aligned}

The overflow (ninth bit) is simply discarded, and the result is correct.

Single zero representation

Unlike some other signed representations, two's complement has only one representation for zero (all bits are 0). This eliminates comparison ambiguity and simplifies equality testing.

No special cases for negation

Finding the negative of any number follows the same simple algorithm: invert all bits and add 1. This works correctly for all values except the most negative number, which has no positive counterpart in the same bit width.

Comparison with other representations

Sign-magnitude

In sign-magnitude representation, the leftmost bit indicates the sign, and the remaining bits represent the absolute value.

Representation+5 (8-bit)-5 (8-bit)
Sign-magnitude0000010110000101
Two's complement0000010111111011

Sign-magnitude has two representations for zero (+0 and -0) and requires separate hardware for signed operations.

One's complement

One's complement negates a number by simply inverting all bits (without adding 1).

Representation+5 (8-bit)-5 (8-bit)
One's complement0000010111111010
Two's complement0000010111111011

One's complement also has two zeros and requires end-around carry for arithmetic operations.

Offset binary (excess-N)

Offset binary adds a fixed bias to values. For example, excess-128 for 8-bit adds 128 to all values, mapping the range [-128, 127] to [0, 255].

This representation is common in analog-to-digital converters and floating-point exponents but is awkward for general integer arithmetic.

Special values and edge cases

Zero

Zero is represented as all bits being 0:

0=000000002 (8-bit)0 = 00000000_2 \text{ (8-bit)}

This is the only representation for zero in two's complement.

Maximum positive value

The largest positive value has a 0 sign bit followed by all 1s:

127=011111112 (8-bit)127 = 01111111_2 \text{ (8-bit)}

Minimum negative value

The most negative value has a 1 sign bit followed by all 0s:

128=100000002 (8-bit)-128 = 10000000_2 \text{ (8-bit)}

This value has no positive counterpart that can be represented in the same bit width. Negating it (using the invert-and-add-1 method) produces the same bit pattern.

Overflow

When arithmetic results exceed the representable range, overflow occurs:

  • Adding two positive numbers may produce a negative result
  • Adding two negative numbers may produce a positive result
  • Adding a positive and negative number never overflows

Detecting overflow: If the sign of both operands is the same but differs from the result's sign, overflow has occurred.

Sign extension

When converting a two's complement number to a larger bit width, you must perform sign extension: copy the sign bit to fill the new high-order bits.

For example, extending -5 from 8-bit to 16-bit:

5 (8-bit)=1111101125 (16-bit)=11111111111110112\begin{aligned} -5 \text{ (8-bit)} &= 11111011_2 \\ -5 \text{ (16-bit)} &= 1111111111111011_2 \end{aligned}

The sign bit (1) is copied eight times to the left. For positive numbers, zeros are added:

5 (8-bit)=0000010125 (16-bit)=00000000000001012\begin{aligned} 5 \text{ (8-bit)} &= 00000101_2 \\ 5 \text{ (16-bit)} &= 0000000000000101_2 \end{aligned}

Practical applications

Programming languages

Most programming languages use two's complement for their signed integer types:

  • C/C++: int, short, long
  • Java: byte, short, int, long
  • Python: Internally uses arbitrary precision, but bitwise operations assume two's complement
  • Rust: i8, i16, i32, i64, i128

Understanding two's complement helps explain behavior like:

  • Why INT_MIN equals -INT_MIN (overflow)
  • How bitwise NOT relates to negation: ~x == -x - 1
  • Why arithmetic right shift preserves the sign

Computer architecture

CPU arithmetic logic units (ALUs) are designed around two's complement. A single adder circuit handles addition of both positive and negative numbers, and the same circuit performs subtraction by negating the second operand.

Embedded systems

When working with hardware registers, sensor data, or communication protocols, you'll often encounter fixed-width two's complement values. Understanding the representation is essential for correctly interpreting and manipulating these values.

Digital signal processing

Audio samples, image pixels, and other signal data are frequently stored as two's complement integers. Converting between formats requires proper sign extension and range handling.

Common operations

Negation

To negate a two's complement number:

x=NOT(x)+1-x = \text{NOT}(x) + 1

Or equivalently:

x=NOT(x1)-x = \text{NOT}(x - 1)

Absolute value

x={xif x0xif x<0|x| = \begin{cases} x & \text{if } x \geq 0 \\ -x & \text{if } x < 0 \end{cases}

Be cautious: The absolute value of the minimum negative number cannot be represented in the same bit width.

Testing the sign

Check if the most significant bit is 1:

is_negative = (x >> (n-1)) & 1

Or compare to zero:

is_negative = x < 0

Hexadecimal representation

Two's complement values are often displayed in hexadecimal for brevity. Each hex digit represents exactly 4 bits:

Decimal8-bit binaryHexadecimal
127011111110x7F
1000000010x01
0000000000x00
-1111111110xFF
-128100000000x80

Note that negative values have hex representations where the first digit is 8-F (indicating the sign bit is 1).

Limitations and considerations

  1. Asymmetric range: There's one more negative value than positive values
  2. Overflow hazard: The most negative value cannot be safely negated
  3. Bit width matters: The same bit pattern has different values in different widths
  4. Implicit assumptions: Many operations assume a specific bit width

When working with two's complement, always be aware of your bit width and handle edge cases appropriately. Modern compilers and languages generally handle these details, but low-level programming requires careful attention to representation limits.