Bit Masking

A bit at position k can be set by shifting 1 left by k positions and ORing the mask with the number. A bit at position k can be cleared by inverting the mask and ANDing it with the number. The expression 1 shifted left by k creates a numbe…

1 sources - 5 claims

A bit at position k can be set by shifting 1 left by k positions and ORing the mask with the number. A bit at position k can be cleared by inverting the mask and ANDing it with the number. The expression 1 shifted left by k creates a number with only bit k set. An inverted mask has every bit set to 1 except the target position. OR sets the target bit while leaving other bits unchanged.