#include <stdio.h> #include <stdint.h> uint8_t is_bit_set(uint8_t reg, uint8_t pos) { // Your code here return reg>>pos&1?1:0; } int main() { uint8_t reg, pos; scanf("%hhu %hhu", ®, &pos); printf("%u", is_bit_set(reg, pos)); return 0; }
consider example 1.
Input: reg = 0b00000100, pos = 2 Output: 1
return reg>>pos&1?1:0;
reg>>pos&1 --> here i am checking bit set or not
reg>>pos&1
reg>>pos --> here i am right shifting for checking position.
reg>>pos
&1 --> here i am and operation between (a & 1) a --> reg>>pos
&1
if it is set it will return true(1).
else it will return false(0).
Test Cases
Test Results
Input
4 2
Expected Output
1