Demo: 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. Keep shift counts below the type's width; shifting by that width or more is undefined.
c
#include <stdio.h>
int main(void) {
unsigned int third = 1u << 3;
unsigned int flags = 0xAu; /* 1010 */
printf("Mask: %u\n", third);
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 & 1u selects that single bit after the shift. Bitwise & is not logical &&, which tests whole expressions for truth.