Uni-Sine
Computer ScienceData and logic

Two's complement converter

A two's complement calculator converts between ordinary numbers and the signed binary that computers use for negative integers. In two's complement the leftmost bit counts as negative, so in 8 bits 1101 0110 is −128 + 64 + 16 + 4 + 2 = −42. Our Two's Complement Converter works at 8, 16 or 32 bits from decimal, binary or hex, lets you flip any bit, and has an IEEE 754 mode for floating-point numbers.

It opens on −42 in 8-bit two's complement: 1101 0110, or D6 in hex.

Try it

Two's Complement Converter

Convert signed binary and floating point numbers

Go to its page

How to use the two's complement converter

  1. Choose Two's complement or IEEE 754.
  2. Pick the word size: 8, 16 or 32 bits.
  3. Choose whether you are typing Decimal, Binary or Hex, and type the number.
  4. Read the bit pattern, the signed value, the unsigned value and the hex, and follow the working: the flip-the-bits-and-add-one method.
  5. Tap any bit to flip it and watch the value change.

The calculator tells you when a number does not fit: 8 bits hold −128 to 127, so typing 128 gives a message rather than a wrong answer. Practice and help sets two's complement and IEEE 754 questions with worked solutions.

How two's complement works

In n bits, the leftmost bit has place value −2ⁿ⁻¹ and the rest are positive as usual. For 8 bits:

−1286432168421
11010110

The bits above are −42: −128 + 64 + 16 + 4 + 2 = −42.

Word sizeSmallestLargest
8 bits−128127
16 bits−32,76832,767
32 bits−2,147,483,6482,147,483,647

Example 1: decimal to two's complement

Write −42 in 8 bits. Write +42 in binary, 0010 1010, flip every bit to get 1101 0101, and add 1: 1101 0110. The calculator gives the same, with D6 in hex and 214 as the unsigned value.

Example 2: two's complement to decimal

What is 1000 0001? The leftmost bit is worth −128, and the last bit 1: −128 + 1 = −127. Choose Binary and type 10000001 to check. A pattern starting with 1 is always negative.

Example 3: a wider word

−300 does not fit in 8 bits, so choose 16 bits: 1111 1110 1101 0100, or FED4 in hex. Adding more 1s to the left of a negative number keeps its value, which is called sign extension.

Check yourself:

practice
Pick an answer, then check it.
What is −1 in 8-bit two's complement?

IEEE 754 floating point

Decimals are stored as floating point: a sign bit, an exponent and a fraction. In IEEE 754 single precision (32 bits) there is 1 sign bit, 8 exponent bits with a bias of 127, and 23 fraction bits. Switch to IEEE 754 to see each field.

Example 4: −6.25 as a float

−6.25 = −1.5625 × 2². The sign bit is 1, the exponent is 2 + 127 = 129 = 1000 0001, and the fraction is the .5625 part, 1001 0000…. In hex the whole word is C0C80000. The calculator shows all three fields and the exact value stored, −6.25, which fits exactly.

Type 0.1 and the exact value stored is 0.100000001490116119384765625, not 0.1: like ⅓ in decimal, 0.1 never ends in binary. That is why computers give answers such as 0.1 + 0.2 = 0.30000000000000004 in double precision.

What the converter does not do

  • No arithmetic. It converts single numbers; it does not add or subtract two binary numbers or show overflow in a sum. Add by hand, then check the result here.
  • Single precision only for floats. IEEE 754 mode is 32-bit; double precision (64-bit) is not shown.
  • No other signed formats. Sign and magnitude and one's complement are not converted, though the guide above shows how they differ.

Common mistakes

  • Forgetting to add 1. Flipping the bits alone gives one's complement, which is one too small in size.
  • Using sign and magnitude. 1010 1010 is not −42; in two's complement it is −86.
  • Ignoring the word size. 1101 0110 is −42 in 8 bits, but 214 as an unsigned byte or in 16 bits with leading zeros.
  • Missing the range. 8 bits cannot hold +128.

Practise data representation in a short timed paper

Two's complement and floating point come up in A-level Computer Science practice. Each daily paper is a short timed paper of 8 questions worth 25 to 35 marks, and the papers mix topics. Our binary calculator guide and hex to decimal converter guide cover unsigned numbers.

Common questions

How do you convert a negative number to two's complement?
Write the positive number in binary, flip every bit, then add 1. −42 in 8 bits is 1101 0110.
How do you convert two's complement to decimal?
Add the place values of the 1 bits, counting the leftmost bit as negative: −128 in 8 bits.
What is the range of 8-bit two's complement?
−128 to 127.
Why do computers use two's complement?
Addition works the same way for positive and negative numbers, and there is only one zero.
What is IEEE 754?
The standard for storing floating-point numbers. Single precision uses 1 sign bit, 8 exponent bits and 23 fraction bits.
Why is 0.1 not stored exactly?
It has no finite binary expansion, so the nearest representable value is stored: 0.100000001490116… in single precision.

Revision cards

Say the answer first, then flip the card to check it.

Where to go next

  • Solve it

    Two's Complement Converter

    Convert signed binary and floating point numbers

    Open the tool
  • Test it

    A-level Computer Science

    A new exam-style paper every day, with instant marking and a mark scheme

    Start today's paper
Read next
Computer Science · Data and logicBinary calculator: convert and add binary numbersConvert binary to decimal, hex and octal and back, and add binary numbers with the carries shown, with the working and examples to check.