I want to get a function that will set the n
last bits of a numerical type to 1
. For example:
bitmask (5) = 0b11111 = 31
bitmask (0) = 0
I, first, had this implementation (mask_t
is just a typedef
around uint64_t
):
mask_t bitmask (unsigned short n) {
return ((((mask_t) 1) << n) - 1;
}
Everything is fine except when the function hit bitmask (64)
(the size of mask_t
), then I get bitmask (64) = 0
in place of 64 bits set to 1
.
So, I have two questions:
-
Why do I have this behavior ? Pushing the
1
by 64 shifts on the left should clear the register and remain with0
, then applying the-1
should fill the register with1
s… -
What is the proper way to achieve this function ?
You need to sign in to view this answers
Leave feedback about this