Find value of k-th bit in binary representation

Given a number n and k (1 <= k <= 32), find value of k-th bit in binary representation of n. Bits are numbered from right (Least Significant Bit) to left (Most Significant Bit).

Examples :

Input :  n = 13, k = 2
Output : 0
Binary representation of 13 is 1101.
Second bit from right is 0.

Input :  n = 14, k = 3
Output : 1
Binary representation of 14 is 1110.
Third bit from right is 1.

1) Find a number with all 0s except k-th position. We get this number using ( 1 << (k-1) ). For example if k = 3, then ( 1 << 2) gives us (00..00100).
2) Do bitwise and of above obtained number with n to find if k-th bit in n is set or not.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find k-th bit from right
#include <bits/stdc++.h>
using namespace std;
  
void printKthBit(unsigned int n, unsigned int k)
{
     cout << (n & (1 << (k-1)));
}
  
int main()
{
  unsigned int n = 13, k = 2;
  printKthBit(n, k);
  return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find
// k-th bit from right
import java.io.*;
  
class GFG 
{
    static void printKthBit(long n, long k)
    {
    System.out.println( (n & (1 << (k - 1))));
    }
  
  
// Driver Code
public static void main (String[] args)
{
    long n = 13, k = 2;
    printKthBit(n, k);
}
}
  
// This code is contributed by anuj_67.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find 
# k-th bit from right
def printKthBit(n, k):
  
    print(n & (1 << (k-1)))
  
n = 13
k = 2
printKthBit(n, k)
  
# This code is contributed 
# by Smitha

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find k-th bit from right
using System;
  
class GFG {
      
    static void printKthBit(long n, long k)
    {
        Console.WriteLine( (n & (1 << 
                            (int)(k - 1))));
    }
  
    // Driver Code
    public static void Main ()
    {
        long n = 13, k = 2;
          
        printKthBit(n, k);
    }
}
  
// This code is contributed by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find 
// k-th bit from right
  
function printKthBit($n, $k)
{
    echo ($n & (1 << ($k - 1)));
}
  
// Driver Code
$n = 13; $k = 2;
printKthBit($n, $k);
  
// This code is contributed
// by anuj_67.
?>

chevron_right


Output :

0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 : vt_m, Smitha Dinesh Semwal



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.