How many bits wide is an int
? You seem to think it's three bits wide. Certainly not correct! Guess again. What is ~0u
? Try printf("%u\n", ~0u);
. What about ~1u
? ... and ~2u
? Do you notice a pattern?
Note the u
suffix, which tells the compiler that it's an unsigned
literal. You can't work with signed integer types with the ~
operator... Well, you can, but you might run into trap representations and negative zeros, according to 6.2.6.2 of n1570.pdf. Using a trap representation is undefined behaviour. That might work on your system, but only by coincidence. Do you want to rely upon coincidence?
Similarly, I suggest using the %u
directive to print unsigned
values, as %d
would produce undefined behaviour according to 7.21.6.1p29 of n1570.pdf.