For a 32 bit integer, how do I set say k low order bits in C?
Assuming you want to set the k lowest bits of a 32-bit integer x, I believe this will work:
if( k > 0 ) {
x |= (0xffffffffu >> (32-k))
}
To set n
least significant bits in k
, you could use arithmetic:
k |= (1 << n) - 1;
(Provided n
is less or equal your int size in bits.)
something along the lines of
set k lower bits:
while (k) {
k--;
num |= (1<<k);
}
Is that what you meant?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With