Setting a bit
Use the bitwise OR operator (
|
) to set a bit.number |= 1 << x;
That will set bit
x
.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.Toggling a bit
The XOR operator (
^
) can be used to toggle a bit.number ^= 1 << x;
That will toggle bit
x
.Checking a bit
You didn't ask for this but I might as well add it.
To check a bit, AND it with the bit you want to check:
bit = number & (1 << x);
That will put the value of bit
x
into the variable bit
.
1 comments:
I am really feeling quite cheerful and motivated after reading this content. You shared wonderful research-based information. I started working at Eiliana.com as a freelancer, an emerging freelancing platform where there are ample clients who wish to hire a programmer for their projects. The work environment is wonderful, and I love working here. I suggest my aspiring buddies join them.
Post a Comment