Computer science

Two's Complement Calculator

Convert a signed integer to two's complement (and one's complement and sign-magnitude) with the number of bits you choose, or decode a bit string, with every step shown.

Step by step

    What is two's complement?

    Two's complement is the way virtually every processor stores signed integers. With n bits, the most significant bit is not worth +2n−1 but −2n−1; all the other bits add up as usual. With 8 bits, 1000 0000 is −128 and 1111 1111 is −128 + 127 = −1.

    Its big advantage is that addition and subtraction use exactly the same circuit as unsigned numbers: you never need to check the sign before operating, there is only one zero, and the range is asymmetric, from −2n−1 to 2n−1 − 1.

    How to convert a negative number to two's complement

    1. Write the absolute value in binary using n bits.
    2. Invert every bit (that is the one's complement).
    3. Add 1.

    Example · −45 in 8 bits

    45 = 0010 1101 → inverted 1101 0010 → +1 = 1101 0011. Check: −128 + 64 + 16 + 2 + 1 = −45.

    Mental shortcut: scan the bits from right to left, copy them up to and including the first 1, then invert all the remaining bits.

    How to convert two's complement back to decimal

    If the first bit is 0, read the number as ordinary binary. If it is 1, either give the first bit a weight of −2n−1 and add the rest, or invert all the bits, add 1 and put a minus sign in front. Both methods give the same result; the calculator shows both.

    Two's complement vs other signed representations

    RepresentationRange with n bitsZerosHow to negate
    Sign-magnitude−(2n−1−1) … 2n−1−1Two (+0 and −0)Flip the sign bit
    One's complement−(2n−1−1) … 2n−1−1TwoInvert every bit
    Two's complement−2n−1 … 2n−1−1OneInvert and add 1
    Excess-2n−1 (bias)−2n−1 … 2n−1−1OneStore value + 2n−1

    Excess (biased) notation is what floating-point exponents use; see it in action in the IEEE 754 converter.

    Overflow when adding

    If you add two numbers with the same sign and the result has the opposite sign, an overflow has occurred: the true result does not fit in n bits. Adding two numbers with different signs never overflows. The carry out of the last bit is not an error in two's complement; it is simply discarded.

    Example · 100 + 50 in 8 bits

    0110 0100 + 0011 0010 = 1001 0110. Both operands are positive but the result starts with 1: overflow. Read as two's complement it is −106 instead of 150, which is outside the −128…127 range.

    Frequently asked questions

    Why does −128 have no positive counterpart in 8 bits?

    There are 256 combinations and one of them is used for zero. That leaves 255: 128 negative and 127 positive. If you negate −128 (1000 0000) by inverting and adding 1, you get 1000 0000 again.

    How do I extend a number to more bits?

    Use sign extension: copy the sign bit to the left. −45 is 1101 0011 in 8 bits and 1111 1111 1101 0011 in 16 bits. Padding with zeros would change the value.

    What is the difference between carry (C) and overflow (V)?

    C is the carry out of the most significant bit and matters for unsigned arithmetic. V means the signed result does not fit. The same addition can set one flag, the other, both or neither.

    Is this the same as "2's complement" in my textbook?

    Yes. Two's complement, 2's complement and twos complement are all names for the same representation.

    More computer science tools