Move a Bit into Position
Move a bit into place
The panel numbers its positions starting at zero. 1u << 3 moves a set bit to position 3 and makes a mask worth 8. Shifting an unsigned value right moves its bits toward lower positions and fills the high positions with zeroes.
A shift count must be nonnegative and smaller than the width of the promoted left operand. Here the operand is unsigned int, and counts 0 through 3 are safe on every C implementation. Do not shift by the width itself. Use unsigned operands for these masks: signed left shifts can have undefined behavior, including when the result is not representable. The left shift here keeps its set bit in range.
The right shift drops the three lowest bits; in 1010, that discards the set bit at position 1 and moves the set bit at position 3 down to position 0.
#include <stdio.h>
int main(void) {
unsigned int bit_three = 1u << 3;
unsigned int flags = 0xAu; /* 1010 */
printf("Mask: %u\n", bit_three);
printf("Bit 3: %u\n", (flags >> 3) & 1u);
return 0;
}
In 0xA (1010), bit 3 is on, so the second printed value is 1. The shift brings that bit to position 0; & 1u discards every other position. That final mask matters if the original value also has bits set above position 3: shifting alone would leave those bits in the result.