user968000 user968000 -4 years ago 59
C Question

why is the bitwise complement of 10 is -11

Why the result of ~10 is -11? Shouldn't it be 5 as the ~ operations flips every bit.

10 = 1010

~10= 0101 //5 in decimal

#include<stdio.h>

int main()
{

unsigned int b =10;
b= ~b;
printf("bitwise %d\n ",b);
return 0;
}

Answer Source

Don't forget that you are flipping every bit, including the most significant bit (sign bit for signed variables). Defining a variable as unsigned doesn't change the binary representation, only how the program uses it.

So when you print the number out as signed (%d) you will get -11.

 10 = 0x0000000A (10)
~10 = 0xFFFFFFF5 (-11 signed / 4294967285 unsigned)

To print out as unsigned use:

printf("bitwise %u\n ",b); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download