I have the following code in c:
unsigned int a = 60; /* 60 = 0011 1100 */ int c = 0; c = ~a; /*-61 = 1100 0011 */ printf("c = ~a = %d\n", c ); c = a << 2; /* 240 = 1111 0000 */ printf("c = a << 2 = %d\n", c ); The first output is -61 while the second one is 240. Why the first printf computes the two's complement of 1100 0011 while the second one just converts 1111 0000 to its decimal equivalent?
cwould have a value of1111 1111 1111 1111 1111 1111 1100 0011.~complements all the bits, not just those you list in the first line.~a = 11111111 11111111 11111111 11000011, buta << 2 = 00000000 00000000 00000000 11110000.c, the value being printed, is declared as signed, and the%format code is for signed printing, socis naturally interpreted as a signed 2's complement number.