Tuesday, 17 September 2013

Bit masks and long-long

Bit masks and long-long

I have a masking procedure (which creates an all-ones bit mask for the
bottom half for a given size):
template<class T>
T bottom_half() {
T halfway = ((sizeof(T) * 8) / 2);
T mask = (1 << halfway) - 1;
return mask;
}
which works fine if I call bottom_half<int>() or long or char. But for
some reason when I run it with long long, halfway is correctly set to 32,
but mask is 0. Why would that be?

No comments:

Post a Comment