Given a number n, check if k-th 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 value of k-th 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> using namespace std; void isKthBitSet(int n, int k) { if (n & (1 << (k - 1))) cout << "SET"; else cout << "NOT SET"; } // Driver code int main() { int n = 5, k = 1; isKthBitSet(n, k); return 0; } |
Java
// Java program to check if k-th bit // of a given number is set or not class Number { public static void isKthBitSet(int n, int k) { if ((n & (1 << (k - 1))) > 0) System.out.print("SET"); else System.out.print("NOT SET"); } // driver code public static void main(String[] args) { int n = 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 def isKthBitSet(n, k): if n & (1 << (k - 1)): print( "SET") else: print("NOT SET") # Driver code n = 5k = 1isKthBitSet(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. using System; class GFG { public static void isKthBitSet(int n, int k) { if ((n & (1 << (k - 1))) > 0) Console.Write("SET"); else Console.Write("NOT SET"); } // Driver code public static void Main() { int n = 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 function isKthBitSet($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 ?> |
Output:
SET
Method 2 (Using Right Shift Operator)
If we right shift n by k-1, we get last bit as 1 if k-th 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> using namespace std; void isKthBitSet(int n, int k) { if ((n >> (k - 1)) & 1) cout << "SET"; else cout << "NOT SET"; } // Driver code int main() { int n = 5, k = 1; isKthBitSet(n, k); return 0; } |
Java
// Java program to check if // k-th bit of a given number // is set or not using right // shift operator. import java.io.*; class GFG { static void isKthBitSet(int n, int k) { if (((n >> (k - 1)) & 1) > 0) System.out.println("SET"); else System.out.println("NOT SET"); } // Driver code public static void main (String[] args) { int n = 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. def isKthBitSet(n, k): if ((n >> (k - 1)) and 1): print("SET") else: print("NOT SET") # Driver code n, k = 5, 1isKthBitSet(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 using System; class GFG { static void isKthBitSet(int n, int k) { if (((n >> (k - 1)) & 1) > 0) Console.WriteLine("SET"); else Console.WriteLine("NOT SET"); } // Driver code static public void Main () { int n = 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. function isKthBitSet($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 ?> |
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 contribute.geeksforgeeks.org or mail your article to contribute@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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check whether the bit at given position is set or unset
- Queries for number of array elements in a range with Kth Bit Set
- Smallest number exceeding N whose Kth bit is set
- Check whether the two numbers differ at one bit position only
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Invert the Kth most significant bit of N
- Inserting m into n such that m starts at bit j and ends at bit i.
- Count of 1-bit and 2-bit characters in the given binary string
- Check if two numbers are bit rotations of each other or not
- Check whether Strings are k distance apart or not
- Check Whether a number is Duck Number or not
- Check whether all the bits are unset in the given range or not
- Check whether a given string is Heterogram or not
- Check whether bitwise AND of a number with any subset of an array is zero or not
- Check whether the given number is Wagstaff prime or not
- Check whether count of distinct characters in a string is Prime or not
- Check whether the frequencies of all the characters in a string are prime or not
- Check whether two strings are equivalent or not according to given condition
- Check whether the Average Character of the String is present or not

