Search code examples
cbinaryintegerunsignedtwos-complement

FInd Two's Complement for Unsigned Integer


I have a simple function that will find the two's Complement for an unsigned integer and then test it to make sure that it is correct

unsigned twoscomplement(unsigned v) {
};

int main()
{
    unsigned a = 255;
    unsigned c = twoscomplement(a);
    unsigned s = a+c;
    printf("%u+%u=%u\n", a, c, s);
    return 0;
} 

When I asked about how I would go around solving this I got the answer unsigned c = (~a)+1; From what I understood (~a) flips the bits and then +1 is for the overflow? Any help on this matter would be appreciated


Solution

  • Whenever we work with one’s complement or two’s complement, we need to state what the word size is. If there are w bits in the word, then the one’s complement of a number x is obtained by subtracting x from the binary numeral made of w 1s. If w is 16, we use 11111111111111112, which is 65535. Then the one’s complement of x is 11111111111111112x. Viewing x as a binary numeral (with at most w bits), whatever bits are on in x will be off in 11111111111111112x, and whatever bits are off in x will be on in 11111111111111112x. Hence, all the bits are complemented.

    C has an operator for the one’s complement; ~x flips all the bits, so it produces the one’s complement of x.

    The two’s complement of x is 2wx, by definition (except that the two’s complement of 0 is 0). 2w equals one plus that binary numeral made of w 1s. For example, 216 = 65535 + 1. Therefore, the two’s complement is one more than the one’s complement. Therefore the two’s complement of x is ~x + 1.

    C also has an operator for the two’s complement, for unsigned integers. Unsigned arithmetic is defined to “wrap” modulo 2w; whenever a regular arithmetic result is outside that range, it is brought back into that range by adding or subtracting 2w as needed. The regular arithmetic negation of x would be negative (if x is not zero), so the computed result of -x is −x + 2w = 2wx, which is the two’s complement of x.