#include <stdio.h> #include <stdint.h> uint8_t is_bit_set(uint8_t reg, uint8_t pos) { return (1 & (reg >> pos)); } int main() { uint8_t reg, pos; scanf("%hhu %hhu", ®, &pos); printf("%u", is_bit_set(reg, pos)); return 0; }
use right shift operator ( >>)
The right shift operator >> is used to "bring down" the bit we are interested in, into the least significant bit (LSB) position.That way, the result will always be 0 or 1.
>>
0
1
Test Cases
Test Results
Input
4 2
Expected Output