c++printfbitwise-operatorsinteger-promotion

Why the sequence from the bitwise operator(~) would be this? Is that broken?


#include <stdio.h>
#include <stdlib.h>

int main() {
unsigned char a=100,b=50;

printf("%d & %d = %d\n",a,b,a&b);
printf("%d | %d = %d\n",a,b,a|b);
printf("%d ^ %d = %d\n",a,b,a^b);
printf(" ~%d = %d\n",a, ~a);       /*the out come of this  line would be this: ~100 = -101 */
printf(" %d >> 2= %d\n",a, a>>2);
printf(" %d << 2= %d\n",a, a<<2);
system("pause");
return 0;
}

/the out come should be 155 ,isn't it?/


Solution

  • According to the standard, the operand of ~ will undergo integral promotion. So here we will first promote a to int.

    [expr.unary.op]: The operand of ~ shall have integral or unscoped enumeration type; the result is the ones' complement of its operand. Integral promotions are performed.

    If int is 4 bytes (for example), the value of the promoted a is 0x00000064. The result of ~a is 0xFFFFFF9B, which is exactly -101(If using two's complement to represent integers).

    Please note that although variadic arguments will undergo integral promotion, here ~a is of type int and no additional promotion is required.