All submissions

Check if K-th Bit is Set

 Code

#include <stdio.h>
#include <math.h>

int isKthBitSet(int n, int k) {
    // Write your code here
    int kthbit = pow(2,k);
    return (n & kthbit ? 1 : 0);
}

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    printf("%d", isKthBitSet(n, k));
    return 0;
}

Solving Approach

 

find the value of the kth bit using kth power of 2 (include math.h)

then do bitwise AND with the kthpower: if answer is zero, it was cleared otherwise it was set

 

Loading...

Input

8 3

Expected Output

1