0
is a signed int
(if you wanted it to be unsigned, you'd write 0U
) and therefore ~0
is also a signed int
.
If your machine uses a 2's-complement representation, then that will have the value -1. The vast majority of machines -- possibly all the machines you will ever see in your career -- are 2's-complement, but technically speaking, ~0
may invoke undefined behaviour if you use it on a machine which uses 1's-complement representation of signed integers and which also prohibits negative zeros.
Even if it may not matter, it's a good idea to get into the habit of only using unsigned integer types with bitwise operators.
Remember that the bitwise operators perform "the integer promotions" on their operands, which means that signed and unsigned short
and char
are automatically promoted to int
-- not unsigned int
(unless it happens that short
is the same width as int
) -- so an explicit cast to unsigned
may be necessary.