
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 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.
Example
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; }
Output
3th significant bit of 12 is : 1
- Related Articles
- What is Least significant bit algorithm in Information Security?
- Find most significant set bit of a number in C++
- Queries for number of array elements in a range with Kth Bit Set using C++
- Print the kth common factor of two numbers
- Program to find Kth bit in n-th binary string using Python
- Map a 10-bit number to 8-bit in Arduino
- Clear/Set a specific bit of a number in Arduino
- 8085 Program to Divide a 16-bit number by an 8-bit number
- 8086 program to divide a 16 bit number by an 8 bit number
- Kth Smallest Number in Multiplication Table in C++
- Kth odd number in an array in C++
- Read a specific bit of a number with Arduino
- Kth prime number greater than N in C++
- Program to find the kth missing number from a list of elements in Python
- Position of the K-th set bit in a number in C++

Advertisements