Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the datatype returned by the bitwise complement operator?

Tags:

c

I try to understand what exactly the bitwise complement operator in C returns. (Not in terms of 'which value', but in terms of 'which datatype'. I guess I understand how the bit-complement theorethically works, so no need to reexplain that)

Consider the following code:

int main(int argc, char **argv){
   char c = 'A';
   printf("%d, %d\n", sizeof(c), sizeof(~c));
   return 0; 
}

While the first sizeof() returns 1, the latter returns 4 on my machine.

So my question is: Of which datatype is ~c?

I'm working on a 32Bit Linux, would the result change to 8 if I'd work on a 64Bit machine? My best guess so far is, that the bitwise operation is applied to a register and the return value is not casted back to a char value.. And can this behaviour be different for other compilers, or does the C standard define what is returned by ~c?

like image 984
Nymokrit Avatar asked Jul 09 '16 17:07

Nymokrit


1 Answers

Operator ~, before the actual operation, performs integer promotions1 on the type,
so the type of ~c is implementation defined, either2int or unsigned int.

Also note that the result of sizeof is the type size_t which should be printed with %zu not %d.


1 (Quoted from ISO:IEC 9899:201x 6.3.1.1 Boolean, characters, and integers 2)
If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. 58) All other types are unchanged by the integer promotions. 58) The integer promotions are applied only: as part of the usual arithmetic conversions, to certain argument expressions, to the operands of the unary +, -, and ~ operators, and to both operands of the shift operators, as specified by their respective subclauses.

2 To clarify why both types and not only int. This is because char may be either signed or unsigned, and it is permitted by the C Standard for unsigned char to not be represented by an int, because the range of the latter could be too small. Had you chosen the type signed char, it could only be promoted to int.

like image 60
2501 Avatar answered Oct 02 '22 15:10

2501