#include <stdio.h>
// Function to check if the bit is set
int isBitSet(unsigned char reg, int pos) {
return (reg & (1 << pos)) ? 1 : 0;
}
int main() {
unsigned char reg;
int pos;
// Input: 8-bit register and position
scanf("%hhu %d", ®, &pos);
// Output: 1 if set, 0 otherwise
printf("%d", isBitSet(reg, pos));
return 0;
}
Use Bitwise AND:
reg & (1 << pos)
isolates the bit at position pos
.1
.0
.
Input
4 2
Expected Output
1