steps to set, clear and toggle Kth bit of N:
task is to set, clear and toggle
K-th bit of this number N.
K-th bit is 0
then set it to 1
if it is 1
leave it unchanged.
bitwise OR of any bit with a set bit results in a set bit, i.e.
bit <bitwise OR> Set bit = Set bit
K is the bit that is to be set
Toggle a bit
XOR of unset
<bitwise XOR> Set bit = Toggle
bitwise XOR of the number with a reset bit is the best idea.
n = n ^ 1 << k
n ^= 1 << k
n | (1 << (k - 1))
Glasp is a social web highlighter that people can highlight and organize quotes and thoughts from the web, and access other like-minded people’s learning.