bit masking tricks

Certainly! Here is a response:

  1. Set a bit: To set a specific bit in a number to 1, use the bitwise OR operator with a mask that has 1 at the bit position to be set.

  2. Clear a bit: To clear a specific bit in a number to 0, use the bitwise AND operator with a mask that has 0 at the bit position to be cleared.

  3. Toggle a bit: To toggle a specific bit in a number (change 1 to 0 or 0 to 1), use the bitwise XOR operator with a mask that has 1 at the bit position to be toggled.

  4. Check if a bit is set: To check if a specific bit is set (equals 1) in a number, use the bitwise AND operator with a mask that has 1 at the bit position to be checked.

  5. Check if a bit is clear: To check if a specific bit is clear (equals 0) in a number, use the bitwise AND operator with a mask that has 0 at the bit position to be checked.