Print Kth least significant bit of a number in C++

In this problem, we are given two numbers n and k. Our task is to print the kth least significant bit of the number n.

Let’s take an example to understand the problem

Input: n = 12 , k = 3
Let’s see the binary representation of n: 12 = 1100

Now, 3rd least significant bit is 1.

To solve this problem we will use the binary bits of the number. And yield the kth bit of the number. For this, we will use binary shifting of the number and left-shift the number (k-1) times. Now on doing end operation of the shifted number and original number which will give the kth bit’s value.


The below code will show the implementation of our solution

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   int N = 12, K = 3;
   cout<<K<<"th significant bit of "<<N<<" is : ";
   bool kthLSB = (N & (1 << (K-1)));
   return 0;


3th significant bit of 12 is : 1

Updated on: 03-Feb-2020


Kickstart Your Career

Get certified by completing the course

Get Started