Check whether K-th bit is set or not | Practice | GeeksforGeeks

Given a number N and a bit number K, check if Kth bit of N is set or not. A bit is called set if it is 1. Position of set bit '1' should be indexed starting with 0 from LSB side in binary representation of the number.

Example 1:
Input: N = 4, K = 0
Output: No
Explanation: Binary representation of 4 is 100, 
in which 0th bit from LSB is not set. 
So, return false.
Example 2:
Input: N = 4, K = 2
Output: Yes
Explanation: Binary representation of 4 is 100, 
in which 2nd bit from LSB is set. 
So, return true.
Example 3:
Input: N = 500, K = 3
Output: No
Explanation: Binary representation of 500 is 
111110100, in which 3rd bit from LSB is not set. 
So, return false.
+1
anishkumargiriak15 hours ago
My solution If you have some suggestion then kindly mention 
class Solution{   public:   // Function to check if Kth bit is set or not.   bool checkKthBit(int n, int k)   {       return(n>>k&1);   }};
0
shrutichintalapudi3 days ago
class CheckBit{   // Function to check if Kth bit is set or not.   static boolean checkKthBit(int n, int k)   {       // Your code here       int tmp=1<<k;       return (n&tmp)==0?false:true;   }   }
0
nv8667172Premium2 weeks ago
#cpp solution 
 
return(n&(1<<(k)));
0
sakshamvedi2 weeks ago

class CheckBit
{
    // Function to check if Kth bit is set or not.
    static boolean checkKthBit(int n, int k)
    {
          n = n>>k;
          if((n&1)==1)
          {
              return true;
          }
          return false;
          
          
          
    }
    
}
0
raunakgiri213 weeks ago
return ((n>>k) & 1)  //using right-shift operator
0
vishnu12653 weeks ago
static boolean checkKthBit(int n, int k)
    {
        while(k>0 && n>0){
            n=n/2;
            k--;
        }
        return (n%2==1);
    }
0
gadarsh7474 weeks ago
if(((n>>k)&1)==1)//Right Shift  
//Left Shift //if(n&(1<<k)) return true;       {           return true;       }       else{           return false;       }
0
atulharsh274Premium1 month ago
return  ( n & (1<<(k)) ) ;
//don't start the index from 1 , (1<<(k-1)) will not work, 
//we have to start the index from 0.
0
mankesh0161 month ago
return n & ( 1 << k );
0
asifsaba5811 month ago
class Solution
{
    public:
    // Function to check if Kth bit is set or not.
    bool checkKthBit(int n, int k)
    {
        // dry run for example 2;
        
        // n=4; 0100
        
        // create a mask 1<<k 0001<<2 = 0100.
        
        // apply & operator in n ans mask = 0100 & 0100 = 0100.
        
        // if any bit is on then it is set 
        
        // else not.
        
        bool bit = n & (1<<k);
        
        return bit;
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.