Conversion

Two's complement calculator

Settings
Reset
Share
Save
Embed
Report a bug

Share calculator

Add our free calculator to your website

Source

Please enter a valid URL. Only HTTPS URLs are supported.

Styling

Input border focus color, switchbox checked color, select item hover color etc.

Advanced

Please agree to the Terms of Use.

Preview

Save calculator

Calculator Settings

Please enter a value within the allowed range.

Please enter a value within the allowed range.

Please enter a value within the allowed range.

Please enter a value within the allowed range.

Share calculator

What is two’s complement?

Two’s complement is the standard way computers store signed integers — numbers that can be positive or negative — using a fixed number of bits. Instead of setting aside a separate symbol for the minus sign, the negative numbers are encoded so that ordinary binary addition just works, with no special handling for the sign. Almost every modern CPU represents integers this way.

This calculator takes a decimal integer and a bit width (8, 16, or 32 bits) and shows its two’s complement pattern in both binary and hexadecimal.

How does it work?

For a chosen width of ww bits, each value is stored as an unsigned bit pattern:

  • If the number nn is non-negative, its pattern is simply the binary of nn, padded with leading zeros to ww bits.
  • If the number nn is negative, its pattern is the binary of 2w+n2^w + n.

Because nn is negative in the second case, 2w+n2^w + n is a positive value smaller than 2w2^w, so it always fits in ww bits. The most significant (leftmost) bit ends up being 11 for every negative number and 00 for every non-negative one — that bit acts as the sign.

Formula

pattern(n)={nif n02w+nif n<0\text{pattern}(n) = \begin{cases} n & \text{if } n \ge 0 \\ 2^{w} + n & \text{if } n < 0 \end{cases}

The result is then written with exactly ww binary digits (or w/4w/4 hexadecimal digits).

Worked examples

Example 1: a positive number

Encode n=5n = 5 in 88 bits. Since 505 \ge 0, the pattern is just 55 in binary, padded to eight digits:

5000001012=0x055 \rightarrow 00000101_2 = \text{0x05}

Example 2: negative one

Encode n=1n = -1 in 88 bits. Since 1<0-1 < 0, compute 28+(1)=2561=2552^8 + (-1) = 256 - 1 = 255:

255111111112=0xFF255 \rightarrow 11111111_2 = \text{0xFF}

Negative one is always an unbroken row of ones, whatever the width.

Example 3: negative five

Encode n=5n = -5 in 88 bits. Compute 28+(5)=2565=2512^8 + (-5) = 256 - 5 = 251:

251111110112=0xFB251 \rightarrow 11111011_2 = \text{0xFB}

Reference table (8-bit)

DecimalTwo’s complement binaryHex
5000001010x05
0000000000x00
-1111111110xFF
-5111110110xFB
127011111110x7F
-128100000000x80

Notes

  • An 88-bit signed integer covers the range 128-128 to 127127; 1616 bits covers 32,768-32{,}768 to 32,76732{,}767; 3232 bits covers roughly ±2.1\pm 2.1 billion. Values outside the chosen range wrap around modulo 2w2^w.
  • The leading bit is the sign: 00 marks a non-negative number and 11 marks a negative one.
  • To convert an ordinary non-negative number to binary without a sign bit, use the decimal to binary converter, or the general number system converter for other bases.

FAQ

What is -1 in two’s complement?

In any width it is all ones: 11111111211111111_2 (0xFF) for 8 bits, 111111111111111121111111111111111_2 (0xFFFF) for 16 bits, and so on.

How do I get the two’s complement of a negative number by hand?

Write the size of the number in binary, flip every bit, then add one. For 5-5 in 8 bits: 55 is 0000010100000101, flipping gives 1111101011111010, and adding one gives 1111101111111011 — the same result as 2565=251256 - 5 = 251.

Why does the bit width matter?

The width fixes how many bits the pattern occupies and therefore the range of numbers you can store. The same decimal value produces a longer string of leading zeros or ones as the width grows.

Report a bug

This field is required.