Skip to content
Related Articles

Related Articles

Improve Article
Find value of k-th bit in binary representation
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2021

Given a number n and k (1 <= k <= 32), find the value of k-th bit in the 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
Explanation:
Binary representation of 13 is 1101.
Second bit from right is 0.

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

Approach:
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. 
 

Below is the implementation of the above approach:

C++




// 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))) >> (k - 1));
}
 
// Driver Code
int main()
{
    unsigned int n = 13, k = 2;
   
    // Function Call
    printKthBit(n, k);
    return 0;
}

Java




// 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))) >> (k - 1)));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        long n = 13, k = 2;
       
        // Function Call
        printKthBit(n, k);
    }
}
 
// This code is contributed by anuj_67.

Python3




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

C#




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

PHP




<?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.
?>

Javascript




<script>
 
// JavaScript program to find k-th bit from right
 
function printKthBit(n, k)
{
    document.write((n & (1 << (k - 1))) >> (k - 1));
}
 
// Driver Code
var n = 13, k = 2;
 
// Function Call
printKthBit(n, k);
 
</script>
Output : 
0

 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes




My Personal Notes arrow_drop_up
Recommended Articles
Page :