3. Check if K-th Bit is Set

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

int isKthBitSet(int n, int k) {
    // Write your code here
    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

Just need to do bit OR with num and 1  shiftting left  with required positions ,if the output is non zero then the k th bit is set and clear for 0

 

 

Was this helpful?
Upvote
Downvote