Given a number n, check if the Kth bit of n is set or not.
Examples:
Input : n = 5, k = 1
Output : SET
5 is represented as 101 in binary and has its first bit set.
Input : n = 2, k = 3
Output : NOT SET
2 is represented as 10 in binary, all higher i.e. beyond MSB, bits are NOT SET.
Method 1 (Using Left Shift Operator) Below are simple steps to find the value of Kth bit:
1) Left shift given number 1 by k-1 to create a number that has only set bit as k-th bit.
temp = 1 << (k-1)
2) If bitwise AND of n and temp is non-zero, then result is SET else result is NOT SET.
Example:
n = 75 and k = 4
temp = 1 << (k-1) = 1 << 3 = 8
Binary Representation of temp = 0..00001000
Binary Representation of n = 0..01001011
Since bitwise AND of n and temp is non-zero, result is SET.
C++
// CPP program to check if k-th bit
// of a given number is set or not
#include <iostream>
usingnamespacestd;
voidisKthBitSet(intn, intk)
{
if(n & (1 << (k - 1)))
cout << "SET";
else
cout << "NOT SET";
}
// Driver code
intmain()
{
intn = 5, k = 1;
isKthBitSet(n, k);
return0;
}
Java
// Java program to check if k-th bit
// of a given number is set or not
classNumber {
publicstaticvoidisKthBitSet(intn,
intk)
{
if((n & (1<< (k - 1))) > 0)
System.out.print("SET");
else
System.out.print("NOT SET");
}
// driver code
publicstaticvoidmain(String[] args)
{
intn = 5, k = 1;
isKthBitSet(n, k);
}
}
// This code is contributed by rishabh_jain
Python3
# Python3 code to check if k-th bit
# of a given number is set or not
defisKthBitSet(n, k):
ifn & (1<< (k -1)):
print( "SET")
else:
print("NOT SET")
# Driver code
n =5
k =1
isKthBitSet(n, k)
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to check if k-th bit
// of a given number is set or not.
usingSystem;
classGFG {
publicstaticvoidisKthBitSet(intn,
intk)
{
if((n & (1 << (k - 1))) > 0)
Console.Write("SET");
else
Console.Write("NOT SET");
}
// Driver code
publicstaticvoidMain()
{
intn = 5, k = 1;
isKthBitSet(n, k);
}
}
// This code is contributed by nitin mittal.
PHP
<?php
// PHP program to check if
// k-th bit of a given
// number is set or not
functionisKthBitSet($n, $k)
{
if($n& (1 << ($k- 1)))
echo"SET";
else
echo"NOT SET";
}
// Driver code
$n= 5; $k= 1;
isKthBitSet($n, $k);
// This code is contributed
// by akt_mit
?>
Javascript
<script>
// Javascript program to check if k-th bit
// of a given number is set or not.
functionisKthBitSet(n, k)
{
if((n & (1 << (k - 1))) > 0)
document.write("SET");
else
document.write("NOT SET");
}
let n = 5, k = 1;
isKthBitSet(n, k);
</script>
Output:
SET
Method 2 (Using Right Shift Operator) If we right shift n by k-1, we get the last bit as 1 if Kth bit is set else 0.
C++
// CPP program to check if k-th bit
// of a given number is set or not using
// right shift operator.
#include <iostream>
usingnamespacestd;
voidisKthBitSet(intn, intk)
{
if((n >> (k - 1)) & 1)
cout << "SET";
else
cout << "NOT SET";
}
// Driver code
intmain()
{
intn = 5, k = 1;
isKthBitSet(n, k);
return0;
}
Java
// Java program to check if
// k-th bit of a given number
// is set or not using right
// shift operator.
importjava.io.*;
classGFG
{
staticvoidisKthBitSet(intn,
intk)
{
if(((n >> (k - 1)) &
1) > 0)
System.out.println("SET");
else
System.out.println("NOT SET");
}
// Driver code
publicstaticvoidmain (String[] args)
{
intn = 5, k = 1;
isKthBitSet(n, k);
}
}
// This code is contributed
// by ajit
Python3
# PHP program to check if k-th bit of
# a given number is set or not using
# right shift operator.
defisKthBitSet(n, k):
if((n >> (k -1)) and1):
print("SET")
else:
print("NOT SET")
# Driver code
n, k =5, 1
isKthBitSet(n, k)
# This code contributed by
# PrinciRaj1992
C#
// C# program to check if
// k-th bit of a given number
// is set or not using right
// shift operator
usingSystem;
classGFG
{
staticvoidisKthBitSet(intn,
intk)
{
if(((n >> (k - 1)) &
1) > 0)
Console.WriteLine("SET");
else
Console.WriteLine("NOT SET");
}
// Driver code
staticpublicvoidMain ()
{
intn = 5, k = 1;
isKthBitSet(n, k);
}
}
// This code is contributed
// by ajit
PHP
<?php
// PHP program to check
// if k-th bit of a given
// number is set or not
// using right shift operator.
functionisKthBitSet($n, $k)
{
if(($n>> ($k- 1)) & 1)
echo"SET";
else
echo"NOT SET";
}
// Driver code
$n= 5; $k= 1;
isKthBitSet($n, $k);
// This code is contributed
// by akt_mit
?>
Javascript
<script>
// Javascript program to check if
// k-th bit of a given number
// is set or not using right
// shift operator.
functionisKthBitSet(n, k)
{
if(((n >> (k - 1)) &
1) > 0)
document.write("SET");
else
document.write("NOT SET");
}
// Driver Code
let n = 5, k = 1;
isKthBitSet(n, k);
// This code is contributed by sanjoy_62.
</script>
Output:
SET
This article is contributed by SAKSHI TIWARI. If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy