Code

#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);
}

int main() {
    uint8_t reg, pos;
    scanf("%hhu %hhu", &reg, &pos);
    printf("%u", is_bit_set(reg, pos));
    return 0;
}

Solving Approach

Step 1: Check the bit status of the desired position

uint8_t bitstatus;
bitstatus = ((reg>>pos)&1);

Step 2: Return 1 if bit is set, otherwise return 0.

return (bitstatus == 1);

 NOTE : The expression can be written in single statement

return (((reg>>pos)&1) == 1);
Upvote
Downvote
Loading...

Input

4 2

Expected Output

1