My question is rather simple, yet I cannot find an easy solution:
I have an integer greater or eqal 16, so at least 1000
in binary. I wish to flip the third bit using the bitwise NOT operator. In this case, it would be 1100
.
Is there any operator which could do this? The ~
-operator flips all bits as far as I know, not just one certain.
XOR with the bits that you want to flip.
int c = 0x10; // 10000b
int m = 0x08; // 01000b
c ^= m; // 11000b
Do
bit_fld ^= (1 << n)
where bit_fld is the bit field, and n=3 for third bit.
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