I did it like that: How do you set, clear and toggle a single bit in C?.
Clearing a bit
Use the bitwise AND operator (
&
) to clear a bit.number &= ~(1 << x);
That will clear bit
x
. You must invert the bit string with the bitwise NOT operator (~
), then AND it.
It works fine with signed integer types, but doesn't work with unsigned integer (e.g. UInt32). Compiler says that it's impossible to do that kind of operation to unsigned types. How to deal with unsigned numbers to clear one bit then ?
It doesn't actually say that. It says "Cannot implicitly convert type 'int' to 'uint'. An explicit conversion exists (are you missing a cast?)"
That's because 1 << x
will be of type int
.
Try 1u << x
.