Old Wolf <
old...@inspire.net.nz> wrote in news:1022b30a-f150-48bf-99b8-
193197...@googlegroups.com:
0 is a signed int value. Bitwise operations depend on the representation
of signed types, which are implementation-specific, so not very reliable.
Also, if you assign the result to a value of potentially larger type
(e.g. unsigned long), it may fail to initialize higher bits. Using ~0U, ~
0UL, ~0ULL would be better.
Actually there is a standard idiom to obtain an unsigned value of any
type with all bits set, namely by converting -1 to this type, e.g.
unsigned long x = -1;
This is guaranteed to work as the C++ standard specifies such conversions
as modulo 2^n, meaning you get an unsigned value of 2^n-1, which means
all bits set. However, this it not very intuitive so may confuse novices.
hth