- Related Questions & Answers
- Add minimum number to an array so that the sum becomes even in C programming
- C/C++ Program for nth Catalan Number?
- C Program to Check if all digits of a number divide it
- Concatenate a string given number of times in C++ programming
- Write a C program to print “ Tutorials Point ” without using a semicolon
- Superperfect Number in C programming
- HTML DOM Input Number value Property
- HTML DOM Input Number type Property
- How to print a formatted text using printf() method in Java?
- How to find the number of days in a month of a particular year in Java?
- Print the string after the specified character has occurred given no. of times in C Program
- C Program for Print the pattern by using one loop
- Print triplets with sum less than or equal to k in C Program
- Print longest palindrome word in a sentence in C Program
- Print n 0s and m 1s such that no two 0s and no three 1s are together in C Program

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

Let’s take an example to understand the problem

Input: n = 12 , k = 3 Output 1 Explanation: 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

#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))); cout<<kthLSB; return 0; }

3th significant bit of 12 is : 1

Advertisements