Given two positive integers n and k. The problem is to check whether the bit at position k from the right in the binary representation of n is set (‘1’) or unset (‘0’).
Constraints: 1 <= k <= number of bits in the binary representation of n.
Examples:
Input : n = 10, k = 2 Output : Set (10)10 = (1010)2 The 2nd bit from the right is set. Input : n = 21, k = 4 Output : Unset
Approach: Following are the steps:
- Calculate new_num = (n >> (k – 1)).
- if (new_num & 1) == 1 then bit is “Set”, else “Unset”.
C++
// C++ implementation to check whether the bit // at given position is set or unset #include <bits/stdc++.h> using namespace std; // function to check whether the bit // at given position is set or unset bool bitAtGivenPosSetOrUnset(unsigned int n, unsigned int k) { int new_num = n >> (k - 1); // if it results to '1' then bit is set, // else it results to '0' bit is unset return (new_num & 1); } // Driver program to test above int main() { unsigned int n = 10, k = 2; if (bitAtGivenPosSetOrUnset(n, k)) cout << "Set"; else cout << "Unset"; return 0; } |
Java
// Java program to // check the set bit // at kth position import java.io.*; class GFG { // function to check whether // the bit at given position // is set or unset static int bitAtGivenPosSetOrUnset ( int n, int k) { // to shift the kth bit // at 1st position int new_num = n >> (k - 1); // Since, last bit is now // kth bit, so doing AND with 1 // will give result. return (new_num & 1); } public static void main (String[] args) { // K and n must be greater than 0 int n = 10, k = 2; if (bitAtGivenPosSetOrUnset(n, k)==1) System.out.println("Set"); else System.out.println("Unset"); } } //This code is contributed by Gitanjali |
Python3
# python implementation to check # whether the bit at given # position is set or unset import math #function to check whether the bit # at given position is set or unset def bitAtGivenPosSetOrUnset( n, k): new_num = n >> (k - 1) #if it results to '1' then bit is set, #else it results to '0' bit is unset return (new_num & 1) # Driver code n = 10k = 2if (bitAtGivenPosSetOrUnset(n, k)): print("Set") else: print("Unset") #This code is contributed by Gitanjali |
C#
// C# program to check the set bit // at kth position using System; class GFG { // function to check whether // the bit at given position // is set or unset static int bitAtGivenPosSetOrUnset( int n, int k) { // to shift the kth bit // at 1st position int new_num = n >> (k - 1); // Since, last bit is now // kth bit, so doing AND with 1 // will give result. return (new_num & 1); } // Driver code public static void Main () { // K and n must be greater // than 0 int n = 10, k = 2; if (bitAtGivenPosSetOrUnset(n, k)==1) Console.Write("Set"); else Console.Write("Unset"); } } // This code is contributed by Sam007. |
PHP
<?php // PHP implementation to check whether the bit // at given position is set or unset // function to check whether the bit // at given position is set or unset function bitAtGivenPosSetOrUnset($n, $k) { $new_num = $n >> ($k - 1); // if it results to '1' then bit is set, // else it results to '0' bit is unset return ($new_num & 1); } // Driver Code $n = 10; $k = 2; if (bitAtGivenPosSetOrUnset($n, $k)) echo "Set"; else echo "Unset"; // This code is contributed by Sam007 ?> |
Output:
Set
Time Complexity: O(1).
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:
- Get the position of rightmost unset bit
- Check whether all the bits are unset in the given range or not
- Check whether all the bits are unset in the given range
- Check whether the two numbers differ at one bit position only
- Set the rightmost unset bit
- Set the Left most unset bit
- Set the rightmost unset bit
- Absolute difference between set and unset bit count in N
- Count of numbers having only one unset bit in a range [L,R]
- Check if a number has same number of set and unset bits
- Check whether K-th bit is set or not
- Position of rightmost set bit
- Find position of the only set bit
- Position of the K-th set bit in a number
- Modify a bit at a given position
- 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
- Find the largest number with n set and m unset bits
- Find the smallest number with n set and m unset bits
- Unset bits in the given range
If you like GeeksforGeeks 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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Sam007

