Tuesday, October 12, 2010

Portable way for setting all bits to true in C

I found this discussion in stackoverflow.com Short summary:
unsigned int flags = -1; /* is portable. */
unsigned int flags = ~0; /*isn't portable because it relies on a two's-complement representation.*/
unsigned int flags = 0xffffffff; /* isn't portable because it assumes 32-bit ints. */
If you want to set all bits in a way guaranteed by the C standard, use the first one.

No comments:

Post a Comment