On Thu, Dec 28, 2017 at 4:35 PM, Dirk Hohndel dirk@hohndel.org wrote:
unsigned int signbit = (1 << (n - 1));
unsigned int mask = (0xFFFFFFFF << n);
unsigned int mask;// shifting a 32bit constant by more than 31 bits has undefined behaviorif (n == 32)mask = 0;elsemask = (0xFFFFFFFF << n);
Ugh. That's ugly.
It's much simpler to just do
unsigned int mask = ~(signbit-1);
instead.
Yes, that mask ends up including the sign bit (the old mask did not), but that doesn't matter for the end result.
Linus