All submissions

Check if K-th Bit is Set

Code

#include <stdio.h>

int isKthBitSet(int n, int k) {
    if (n & (1<<k)){
        return 1;
    }

    return 0;
}

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

Solving Approach

In order to check if a bit is set, we need to use the & operator which will return a 1 only if the bit we are and-ing it with is 1. Therefore, we can use a mask to check if the bit is set, then return 1 if true else return 0.

 

 

Loading...

Input

8 3

Expected Output

1