When you are setting a bit flag (position "p"), what is the mask that you need to use and what is the operation required? What is supposed to be the mask value for reset and what is the operation? Give one example.
A binary OR operation is used when setting the p'th bit on the source and a mask with the p'th bit set to 1 while other bits are set to 0 and the result is stored at the source.
Hence, (1 <<p) is the value of mask.
source = 11011011
p = 3
mask = 2>>1 = 00001000
Mask | source = 11011011
A binary AND operation is performed on the source when resetting the p'th bit and mask will have the p'th bit set to 0 and all other bits set to 1.
Thus, ~(1 << p) is the value of the mask.
Comments
Leave a comment