Sunday, June 8, 2025

Three important bitwise operations in embedded systems

Setting the Nth bit in the register R via the assignment R |= (1 << N): The new value of the register R will contain the result of the bitwise OR operation between its original value and a bitmask containing all zeros, except the bit corresponding to the value we want to set, which is set to the value one.

Clearing (resetting) the Nth bit in the register R via the assignment R &= ~(1 << N): The new value of the register is the result of a bitwise AND operation between its original value and a bitmask containing all ones, except the bit in the position we want to clear, which is set to the value zero.

Checking whether the Nth bit of the register R is set or cleared, via the condition (R & (1 << N) == (1 << N)): Returns true only if the Nth bit of the register is set.

No comments:

Post a Comment