Code

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

uint8_t is_bit_set(uint8_t reg, uint8_t pos) {
   if(reg & (1 << pos)){
    return 1;
   } else {
    return 0;
   }
//    return (reg & (1 << pos)) ? 1 : 0;
}

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

Solving Approach

  • Input: Read reg and pos.
  • Create mask: mask = 1 << pos
  • Check bit:
    • If (reg & mask) != 0 → bit is 1
    • Else → bit is 0
  • Output: Print 1 or 0

 

 

Upvote
Downvote
Loading...

Input

4 2

Expected Output

1