1. Home
  2. Developer tools
  3. Two’s Complement Calculator

Two’s Complement Calculator

Computers store negative integers in two’s complement. Pick a width such as 8, 16, 32 or 64 bits, enter a signed number or a bit pattern, and see the other form, the valid range and a worked explanation.

Free, no sign-up. Runs in your browser: nothing is uploaded.

How to use Two’s Complement Calculator

  1. Choose the bit width.
  2. Enter a signed decimal number, or a binary or hex pattern.
  3. Read the pattern or the signed value and the working.

Features

  • Widths of 4, 8, 16, 32, 64 and 128 bits
  • Decimal to pattern and pattern to decimal
  • Shows the invert-and-add-one working
  • Smallest and largest value for the chosen width, with overflow reported
  • Also lists the one’s complement and sign-magnitude forms
  • Exact 64-bit values using big integers

Supported formats

Input

  • Signed decimal
  • Binary bits
  • Hexadecimal

You get

  • Bit pattern
  • Signed value
  • Working

Private by design

Everything you enter is processed by your browser on this device. Nothing is sent to openWorkstand or anyone else, so the Two’s Complement Calculator is safe for private and work data. After you have used it once while online, it also works without an internet connection. How local processing works

Good to know

  • A value outside the range of the chosen width is reported as an overflow, not wrapped around.
  • Only the widths 4, 8, 16, 32, 64 and 128 bits are offered.

Guide

Two’s complement: how computers store negative integers

Signed integers need a rule for the negative side, and almost every processor uses the same one: two’s complement. The pattern for −x is what you get when you subtract x from 2 to the power n, where n is the number of bits. It has two useful features. Addition and subtraction use the same circuit whatever the signs, and there is only one zero. This page converts in both directions and shows how.

The invert-and-add-one rule

To find the pattern of a negative number, write its size in binary using the chosen width, flip every bit, and add one. For −5 in 8 bits the size is 00000101, the inverted bits are 11111010, and adding one gives 11111011, which is hexadecimal FB. To read a pattern back, check the top bit: if it is 0 the number is positive and you read it as ordinary binary; if it is 1 you invert, add one and negate.

Range and the extra negative number

An n-bit signed integer runs from −2^(n−1) to 2^(n−1) − 1. For 8 bits that is −128 to 127; for 16 bits −32,768 to 32,767; for 32 bits −2,147,483,648 to 2,147,483,647. The page prints the smallest and largest values for the width you picked. If a number lies outside the range, it says so and does not wrap around, because silent overflow is exactly the kind of mistake people use a calculator to catch.

Input types

Choose whether your input is a signed decimal number, a binary pattern or a hexadecimal pattern. Decimal input is turned into bits for the chosen width. Binary and hex input are read as patterns of that width and shown as signed and unsigned numbers. A pattern shorter than the width is padded with zeros on the left, so write the sign bit explicitly when you mean a negative value.

Other forms shown

Beside the answer, the page lists the one’s complement, where every bit is simply flipped, and sign-magnitude, where the top bit is a sign and the rest is the size. These older schemes have two zeros and need extra care in arithmetic, which is why two’s complement won. The value −128 has no sign-magnitude form in 8 bits, and the page marks it as such.

A worked example

The Sample button loads −42 at 8 bits. The size 42 is 00101010, the inverted bits are 11010101, and adding one gives 11010110. In hexadecimal that is D6, and read as an unsigned byte it is 214, which equals 256 − 42. The one’s complement is 11010101 and the sign-magnitude form is 10101010. The range line reads −128 to 127, so the value fits. In 16 bits the same number becomes 1111111111010110, showing how sign extension copies the top bit.

Limits

Only fixed widths of 4, 8, 16, 32, 64 and 128 bits are offered, and the arithmetic is done with big integers, so there is no rounding. The page does not simulate overflow flags or arithmetic operations, and it does not know what type your variable has. When you compare with a program’s output, check that the program uses the same width and signedness. For bit patterns of floating-point numbers use the IEEE 754 converter instead.

Checked against the current version of the Two’s Complement Calculator on .

Frequently asked questions

Why does an 8-bit signed integer go down to −128 but only up to 127?

There are 256 patterns, and half of them have the top bit set, which means negative. Zero uses one of the other half, leaving 127 positive numbers. The extra negative number, −128, has no positive partner, which is why negating it overflows.

How do I read a pattern such as 11110000 as a signed number?

If the top bit is 1, the number is negative. Invert the bits to get 00001111, add one to get 00010000, which is 16, and put a minus sign in front. The pattern is −16. Unsigned, the same bits are 240.

How do I write -5 in 8-bit two’s complement?

Write 5 as 00000101, invert every bit to 11111010 and add 1, giving 11111011. In hex that is 0xFB.

What range does n bits cover?

From -2^(n-1) to 2^(n-1) - 1. For 8 bits that is -128 to 127, and the extra negative value has no positive twin.

Keep working with the result

openWorkstand also includes a free workspace with documents, spreadsheets, tasks, projects and a calendar that save to a folder on your computer.