StatGardenREF. DESK
Calculators/Maths/Two's complement
Maths

Two's complement calculator

Converts a signed decimal integer to its binary two’s-complement form at a chosen bit width.

What this calculator does

Two's complement works out converts a signed decimal integer to its binary two’s-complement form at a chosen bit width. Enter your own figures above and the answer updates as you type: nothing is fixed in the code, so the result reflects exactly the numbers you supply.

The formula this calculator evaluates is printed under the tool and explained below, so you can check the working by hand or reuse it in a spreadsheet.

The formula

FormulaFor a negative number: invert the bits of |n| in binary, then add 1. Range: −2^(bits−1) to 2^(bits−1)−1

The inputs explained

FieldWhat to enter
Decimal integerA number. Starts at -16.
Bit widthA number. Starts at 8.

Worked examples

Every figure in the tables below is produced by this page’s own calculator at build time, so the numbers and the tool always agree. Select any row to load that scenario.

How the answer changes with decimal integer

Every other input is held at the calculator’s starting values while decimal integer varies. Select any row to load that scenario into the calculator.

How the answer changes with decimal integer
Decimal integerTwo's complement (binary)Bit widthRepresentable range
-8111110008-128 to 127
-12111101008-128 to 127
-16111100008-128 to 127
-24111010008-128 to 127
-32111000008-128 to 127
-48110100008-128 to 127