Two's Complement Converter

Convert a signed decimal number to its two's complement bit pattern at 4 to 64 bits, or decode a binary or hex pattern back to decimal, with the steps.

8 bits is a byte (int8); 16, 32 and 64 bits match short, int and long in most programming languages.
A whole number, negative or positive.

Results

8-bit two's complement
1111 0011
Hexadecimal
0xF3
Same bits read as unsigned
243
Steps
Write the size, 13, in binary with 8 bits: 0000 1101 Flip every bit: 1111 0010 Add 1: 1111 0011 Shortcut check: 2⁸ − 13 = 256 − 13 = 243, which is 1111 0011 in binary.
Range of 8-bit two's complement
−128 to 127

How it’s calculated

Computers store signed whole numbers in two's complement. With N bits, the leftmost bit has the negative place value −2ᴺ⁻¹ and every other bit its usual positive value, so a leading 1 means the number is negative. There is only one zero, and the range is lopsided: one more negative number than positive.

value = −b₍N−1₎ × 2ᴺ⁻¹ + b₍N−2₎ × 2ᴺ⁻² + … + b₁ × 2 + b₀ range: −2ᴺ⁻¹ to 2ᴺ⁻¹ − 1 (8 bits: −128 to 127) to negate: flip every bit, then add 1 (same as 2ᴺ − |x|)

Example: −13 in 8 bits. 13 is 0000 1101; flipping the bits gives 1111 0010; adding 1 gives 1111 0011 (hex F3). Reading it back: −128 + 64 + 32 + 16 + 2 + 1 = −13. Read as an unsigned number the same bits are 243, and 243 − 256 = −13.

Frequently asked questions

Why does 1111 1111 mean −1?

In 8-bit two's complement the leftmost bit is worth −128 and the rest add up to 127, so the total is −1. All ones is −1 at every width.

Why is the range −128 to 127 and not −127 to 127?

Zero takes one of the non-negative patterns, so there are 128 negative patterns (leftmost bit 1) and only 127 positive ones. 1000 0000 is −128 and has no positive partner.

How do I sign-extend to more bits?

Copy the leftmost bit into the new positions. −5 is 1011 in 4 bits and 1111 1011 in 8 bits; both mean −5. A positive number gets zeros on the left.

What happens if a number does not fit?

It overflows: 200 cannot be stored as a signed 8-bit number (the maximum is 127). The calculator says so instead of wrapping around; choose a wider bit width.

Embed this calculator

Add this free calculator to your own website. Copy the code below into your page’s HTML:

Need this calculator branded, changed or built around your own prices?See custom business solutions →

Sources

Formulas are taken from the free public references above. Results are provided “as is” for informational and educational purposes only. See our disclaimer.

Added · Last updated · How we check our calculators

Spotted a mistake or missing option? Report a problem· Suggest a calculator