Check if K-th Bit is Set

Code

#include <stdio.h>

// Write a C program to check if the K-th bit (0-based index) of an integer N is set (1) or not (0).
// Input Format:  Two integers N and K.
// Output Format: Print 1 if the K-th bit of Integer N is set (1), otherwise print 0.

#define SET 1 
#define CLR 0 

int check_bit(int n, int pos){
    int x = (n >> pos) & 1 ; 
    if(x){
        return SET; 
    }
    else{
        return CLR; 
    }
}

int main(){
    int n,pos; 
    scanf("%d %d",&n,&pos); 
    printf("%d",check_bit(n,pos)); 

    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

8 3

Expected Output

1