#include <stdio.h>
int isKthBitSet(int n, int k) {
if((n & (1 << k)) != 0 )
return 1;
else
return 0;
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
printf("%d", isKthBitSet(n, k));
return 0;
}
Solving Approach
use bitwise operators efficiently.
Be careful while giving the condition because(n & (1 << k)) does not return 1 for higher bits, it returns powers of 2 (2, 4, 8, …) depending on which bit is set. So the right condition is to check if the result is non-zero, not equal to 1