I have to check whether K-th bit of a number is set or not. Input: N = 4, K = 0 Output: false Explanation: Binary representation of 4 is 100, in which 0th bit from LSB is not set. So, return false.
This code does not work with the not equal to statement:
bool checkKthBit(int n, int k)
{
if(n&(1<<k)!=0)
return true;
else
return false;
}
However after removing the not equal to operator the code works perfectly fine:
bool checkKthBit(int n, int k)
{
if(n&(1<<k))
return true;
else
return false;
}
How is this happening?
Aucun commentaire:
Enregistrer un commentaire