What does the assignment of ~0u to a variable mean in C++?

It indicates bitwise not; all bits in the integer will be flipped, which in this case produces a number where all bits are 1.

Note that since it is unsigned, if the integer is widened during assignment the extended bits would be 0. For example assuming that unsigned short is 2 bytes and unsigned int is 4:

unsigned short s = ~0u; // 0xFFFF
unsigned int i = s;     // 0x0000FFFF

If you need to invert the bits of some generic numeric type T then you can use the construct ~(T(0)).

Leave a Comment