#include <stdio.h> #include <stdint.h> uint8_t is_bit_set(uint8_t reg, uint8_t pos) { return ((reg & (1 << pos)) != 0) ? 1U : 0U; } int main() { uint8_t reg, pos; scanf("%hhu %hhu", ®, &pos); printf("%u", is_bit_set(reg, pos)); return 0; }
1 left shift to position will create a mask. While AND operation is performed resulting in 0 in case of bit is cleared and non-zero value in case of bit is set, therefore terinary operator is used to return 1 or 0 respectively.
Test Cases
Test Results
Input
4 2
Expected Output
1