Two’s complement is the way every computer I know of chooses to represent integers. To get the two’s complement negative notation of an integer, you write out the number in binary. You then invert the digits and add one to the result.

Suppose we’re working with 8 bit quantities (for simplicity’s sake) and suppose we want to find how -28 would be expressed in two’s complement notation. First we write out 28 in binary form.

00011100 Then we invert the digits. 0 becomes 1, 1 becomes 0.

11100011 Then we add 1.

11100100 That is how one would write -28 in 8 bit binary.

Conversion from Two’s Complement

Use the number 0xFFFFFFFF as an example. In binary, that is:

1111 1111 1111 1111 1111 1111 1111 1111 What can we say about this number? It’s first (leftmost) bit is 1, which means that this represents a number that is negative. That’s just the way that things are in two’s complement: a leading 1 means the number is negative, a leading 0 means the number is 0 or positive.

To see what this number is a negative of, we reverse the sign of this number. But how to do that? To reverse the sign you simply invert the bits (0 goes to 1, and 1 to 0) and add one to the resulting number.

The inversion of that binary number is, obviously:

0000 0000 0000 0000 0000 0000 0000 0000 Then we add one. 0000 0000 0000 0000 0000 0000 0000 0001 So the negative of 0xFFFFFFFF is 0x00000001, more commonly known as 1. So 0xFFFFFFFF is -1.

Conversion to Two’s Complement

Note that this works both ways. If you have -30, and want to represent it in 2’s complement, you take the binary representation of 30:

0000 0000 0000 0000 0000 0000 0001 1110 Invert the digits.

1111 1111 1111 1111 1111 1111 1110 0001 And add one.

1111 1111 1111 1111 1111 1111 1110 0010 Converted back into hex, this is 0xFFFFFFE2. And indeed, suppose you have this code:

#include <stdio.h>

int main() {
    int myInt;
    myInt = 0xFFFFFFE2;
    printf("%d\n",myInt);

    return 0;
}

That should yield an output of -30.