Two's Complement Calculator

Free two's complement calculator. Convert a signed decimal to its two's complement bits or read a binary pattern back to decimal, with the invert-and-add-one steps, hex, sign bit, and range.

https://hexacalculator.com/calculators/other/computers/twos-complement-calculator

Other

Computers

Two's Complement Calculator

Free two's complement calculator. Convert a signed decimal to its two's complement bits or read a binary pattern back to decimal, with the invert-and-add-one steps, hex, sign bit, and range.

Two's Complement Calculator

Set up your conversion

Decimal to two's complement: enter a whole number. Positive numbers use their plain binary form; negative numbers are stored by inverting the bits of the magnitude and adding 1.

Choose a direction and a bit width, then enter your value.

Results

In 8-bit two's complement, -50 is written 1100 1110 in binary and 0xCE in hex. Read as an unsigned pattern those bits are 206, and the sign bit is 1.

Sign bit (MSB)
Range minimum
Range maximum

Step by step

The two's complement worked out one step at a time

Step

What happens

Bits

1. Magnitude |-50| = 50in 8 bits0011 0010
2. Invert every bitones' complement1100 1101
3. Add 1gives the two's complement1100 1110
Result-50 in two's complement1100 1110

This value in every form

The same value as signed decimal, unsigned decimal, binary and hex

Representation

Value

Signed decimal-50
Unsigned decimal206
Two's complement binary1100 1110
Hexadecimal0x CE
Sign bit (MSB)1 (negative)
Loading calculator…

A tool for calculating 2's complement binary numbers converts decimal values to the bit patterns that a computer would actually store, and converts the 2's complement pattern back into the corresponding signed value. It shows all steps of the calculation as you enter the value and select the direction and width of conversion.

Two's complement representation is the most common method of representing signed integers on computers, and more generally in computer arithmetic. It allows the use of binary signed numbers, positive and negative, to be treated mathematically as if they were unsigned. This method reduces the complexity of adding negative numbers and has no need for sign-magnitude representation of negative values, which simplifies the internal logic.

To calculate the two's complement of a number expressed in binary, follow these steps:

For negative numbers, this method consists of three steps: write the absolute value in normal binary representation, invert each bit, and then add one. The resulting pattern is the two's complement pattern with the most significant bit being a 1.

Let's take the number -50 as an example in 8-bit representation. First we represent 50 in binary form, then we invert all bits and finally we add 1.

Step

Result

50 in binary (8 bits)

0011 0010

Invert every bit (ones' complement)

1100 1101

Add 1

1100 1110

-50 in two's complement

1100 1110

For non-negative numbers, the bits do not need to be inverted at all. You simply represent the number in binary and pad it with leading zeros until you reach the desired bit width. So for example, the 8-bit two's complement of +50 is simply 00110010, where the sign bit is 0.

How to read two's complement representation:

There are also rules for how to interpret a bit pattern as a signed integer. If the leftmost bit is 0, then the value represents either zero or a positive number and each bit is read as a normal binary bit. If the leftmost bit is 1, then the value represents a negative number.

The simplest and most uniform rule for handling both cases is to give the highest valued bit a negative weight. In an N-bit number each bit has a weight of 2 to the power of its position except for the highest valued bit, which has a weight of 2 to the power of (N - 1).

For the eight-bit pattern 1100 1110, the contribution of the sign bit is negative 128, while the contribution of each of the remaining bits is 64, 8, 4 and 2. The sum is equal to negative 128 plus 78, which gives a result of -50. This corresponds to the original value.

Bit

1

1

0

0

1

1

1

0

Weight

27-2^7

262^6

252^5

242^4

232^3

222^2

212^1

202^0

Value

-128

64

0

0

8

4

2

0

Table for two's complement representations with four bits:

This pattern is easiest to understand with a small bit width. With four bits, sixteen values can be stored, ranging from −8 to 7. All positive numbers start with 0, while all negative numbers begin with 1. Additionally, an entire row of ones represents -1.

Decimal

Two's complement

Decimal

Two's complement

0

0000

-1

1111

1

0001

-2

1110

2

0010

-3

1101

3

0011

-4

1100

4

0100

-5

1011

5

0101

-6

1010

6

0110

-7

1001

7

0111

-8

1000

Why use two's complement representation instead of a sign bit?

A simple way to store negative numbers is to keep the sign bit separate from the value bits. This method, however, results in two representations of zero (positive and negative), and requires different logic for signed addition.

Two's complement solves both of these problems at once. There is only one representation for zero and the binary addition of two numbers works exactly as it should without any special cases for negative numbers. Any carry out of the most significant bit is simply discarded. Subtraction is done by adding the two's complement of the second operand.

The only exception is that the range of values is not symmetrical. Because zero is represented by a pattern of zeroes, there are one more negative numbers than positive numbers. The range for eight bits goes from −128 to 127, rather than −127 to 127.

Value ranges for typical bit widths

For each increase in bit width the number of values that can be stored doubles with positive and negative values being half of those. The following table shows the ranges supported by this tool, as well as common sizes for 32-bit and 64-bit integers.

Bits

Lowest

Highest

Number of values

4

-8

7

16

8

-128

127

256

12

-2048

2047

4096

16

-32768

32767

65536

32

-2147483648

2147483647

about 4.29 billion

64

about -9.2 quintillion

about 9.2 quintillion

about 18.4 quintillion

Smallest Negative Number and Overflow

The smallest value for any bit width is a special value. For eight bits it's 1000 and 0000, or -128. If you try to negate this value and add one, you get back 1000 and 0000. This is because +128 doesn't fit in a signed byte. It's important to take into account this one exception when writing code that negates signs.

Overflow in addition works similarly. When the sum of two numbers with the same sign exceeds the range, the sign bit is flipped and the result "wraps around". For a signed byte, adding 127 to 1 gives 1000 0000, which is interpreted as -128.

The origins of two's complement representation

Even before computers were developed, the complement method was used for subtraction in mechanical calculators. John von Neumann suggested two's complement representation for electronic computing devices in his 1945 First Draft of a Report on the EDVAC and it was actually implemented in the EDSAC computer in 1949.

Early computers used different methods to represent two's complement, inverted bit sequences and sign-magnitude representations. IBM System/360 made the two's complement representation the industry standard in 1964, and since then almost all processors have adopted this standard.

Frequently asked questions

What can this tool do for calculating two's complement representation?

It supports bidirectional conversions. If you enter a decimal number and a bit width it will show the two's complement pattern and list out the steps: taking the magnitude, inverting each bit, adding one. If you enter a binary pattern it will convert back to a signed value showing how the sign bit gets a negative weight.

How do you manually calculate two's complement of a negative number?

The value is represented in binary and padded to the specified bit width. Each bit is inverted, then one is added. For example, for an eight-bit field -50 would be 00110010, after inversion it becomes 11001101, and after adding one it becomes 11001110.

What is the two's complement of 5 in 8 bits?

Since +5 is 00000101, the original value will be stored and the sign bit will be 0. To store -5, we first invert to get 11111010, then add one to get 11111011. If you enter -5 in decimal mode into this tool, it will return 11111011.

Why can only numbers up to 127 be represented with an 8-bit representation and not 255?

This is because half of the possible patterns are reserved for representing negative numbers. Patterns that start with a 0 represent values from 0 to 127, while patterns that start with a 1 represent values from -128 to -1. In contrast, all 256 possible patterns in an unsigned byte are used to represent values from 0 to 255.

What is the two's complement of 1111 1111?

A pattern of ones represents −1 regardless of the bit width. For eight bits, 11111111 is equal to −1 because the sign bit contributes a value of −128 and the sum of the remaining seven bits gives +127, which totals to −1.

Why is the smallest negative number special?

The smallest negative number (e.g., -128 with 8 bits) has no corresponding positive value in the same bit width. If you try to negate it and add 1, you get back the same pattern. This is because +128 does not fit into this range. It's the only value whose sign cannot be flipped without increasing the number of bits.

Does this calculator support hexadecimal?

Although the results are displayed in hexadecimal format, the hexadecimal system is only used for output. The input fields do not accept letters A through F, so you must enter numbers either decimal or binary and then check the corresponding hexadecimal value in the results.

Related calculators

Disclaimer: This calculator is provided for general informational and educational purposes only. Our calculators are under active development, and results may be inaccurate, incomplete, or unsuitable for your situation. Always verify the figures independently and seek advice from a qualified professional before relying on them. We make no warranties and accept no liability for any loss or decision arising from use of this tool.

References

  1. Wikipedia: Two's complement

    Definition, the value formula with a negative-weight sign bit, ranges, and history.

  2. Exploring Binary: Decimal/Two's Complement Converter

    Both conversion directions, bit-width handling, and the four-bit value table.

  3. AllMath: Two's Complement Calculator

    The invert-and-add-one method with a worked decimal example.