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

09 Jun, 2021
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++
Java
Python3
C#
PHP
Javascript
// 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 codeint main(){    int n = 5, k = 1;    isKthBitSet(n, k);    return 0;}
// 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 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 coden = 5k = 1isKthBitSet(n, k) # This code is contributed by "Sharad_Bhardwaj".
// 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 program to check if// k-th bit of a given// number is set or notfunction 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?>
<script>    // Javascript program to check if k-th bit    // of a given number is set or not.         function isKthBitSet(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++
Java
Python3
C#
PHP
Javascript
// 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 codeint main(){    int n = 5, k = 1;    isKthBitSet(n, k);    return 0;}
// 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 codepublic static void main (String[] args){    int n = 5, k = 1;    isKthBitSet(n, k);}} // This code is contributed// by ajit
# 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 coden, k = 5, 1isKthBitSet(n, k) # This code contributed by# PrinciRaj1992
// C# program to check if// k-th bit of a given number// is set or not using right// shift operatorusing 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 codestatic public void Main (){    int n = 5, k = 1;    isKthBitSet(n, k);}} // This code is contributed// by ajit
<?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?>
<script> // Javascript 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) > 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
 
YouTubeGeeksforGeeks502K subscribersCheck whether K-th bit is set or not | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:39•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=006HH_izfPM" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
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. 
nitin mittal
classyallrounder
jit_t
princiraj1992
shreyashagrawal
_merlin__
divyeshrabadiya07
sanjoy_62
Microsoft
Bit Magic
Strings
Microsoft
Strings
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Program to find whether a given number is power of 2
Binary representation of a given number
Bits manipulation (Important tactics)
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
C++ Data Types