Is the Bit Set

NabilMohamed
NabilMohamed

Code

#include <stdio.h>
#include <stdint.h>

uint8_t is_bit_set(uint8_t reg, uint8_t pos) {
    // we can check if a bit is set by ANDing it with 1
    
    // left shift 1 by <pos> bits, then AND it with reg
    // right shift the result by pos bits
    return (reg & (1 << pos)) >> pos;
}

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

Solving Approach

 

 

 

Loading...

Input

4 2

Expected Output

1