I have the below code and I am not sure why negation inside printf is different from doing negation outside?
printf("Value of d= %X\n",~d);
printf("Value of d= %X\n",d);
Before the operation of the
~ operator is performed, the type of the operand it promoted to
int. This means the result of:
~0x04 will be
0xFFFFFFFB in your case, as the width of type
int appears to be 32 bits.
If the value
0xFFFFFFFB is passed to printf it isn't promoted to
int as it is already of that type, and it is printed out.
0xFFFFFFFB is assigned back to
d, a conversion from
char in an implementation-defined manner is done. In your case the resulting value is:
d is passed to printf, it gets promoted to
int, the value of
0xFB when promoted to
int stays the same, and it gets printed out.