All submissions

Check if K-th Bit is Set

Code

#include <stdio.h>

int isKthBitSet(int n, int k) {
    // Write your code here
    //If k-th bit is set in n, print 1
    //If k-th bit is not set in n, print 0
    //Set the k-th bit position and & with number
    n = n & (1<<k);
    if(n == 0){
        //If n is 0, than k-th bit is not set
        return 0;
    }
    //k-th bit is set, so return 1
    return 1;
}

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

Solving Approach

 

 

Loading...

Input

8 3

Expected Output

1