Uh ... In your example, you already know the bit index. Then itβs easy:
bits &= ~(1 << index);
This will close the bit whose index is index , regardless of its position in the value (highest, lowest, or intermediate). Think about it, you can, of course, use the fact that you know that the bit is already set, and use XOR to knock it again:
bits ^= (1 << index);
This saves the inverse, which is probably one machine instruction.
If you want to hide the lowest bit without knowing its index, the trick:
bits &= (bits - 1);
See here .
unwind
source share