Originally Posted by
Memloop
But you're going to have to hard code the bitmask, and that's going to be trouble if CHAR_BITS change. What are you going to do on machines that only have 16 bit bytes?
You do not have to hard code the bit mask, but after thinking more carefully on how I would actually do it, let me guess: when you say "use bit shifting to create a XOR mask", you actually mean to use bit shifting and bitwise xor to create the mask that will be used with bitwise and? That seems to be the simplest solution that I can come up with at this point, e.g.,
Code:
unsigned char mask = 3U << (CHAR_BIT / 2 - 1);
mask ^= ~0U;