All submissions

Check if K-th Bit is Set

Code

#include <stdio.h>

int isKthBitSet(int n, int k) {
    // Write your code here

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

Solving Approach

0000 0000 0000 1000   -> this is the binary of 8.

where k = 3;

n = n>>k&1;

n>>k   --> here i am right shifting "3rd" position to "0th" position.

after that n binary will be 0000 0000 0000 0001

then i am doing "&" operation with "1"

0000 0000 0000 0001  &   0000 0000 0000 0001

here 1 & 1 = 1;

giving output as 1; 

 

 

Loading...

Input

8 3

Expected Output

1