- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find most significant set bit of a number in C++
Here we will see if a number is given, then how to find the value of Most Significant Bit value, that is set. The value is power of 2. So if the number is 10, MSB value will be 8.
We have to find the position of MSB, then find the value of the number with a set-bit at kth position.
Example
#include<iostream> #include<cmath> using namespace std; int msbBitValue(int n) { int k = (int)(log2(n)); return (int)(pow(2, k)); } int main() { int n = 150; cout << "MSB bit value is: "<< msbBitValue(n); }
Output
MSB bit value is: 128
- Related Articles
- Print Kth least significant bit of a number in C++
- Position of the K-th set bit in a number in C++
- Find position of the only set bit in C++
- For every set bit of a number toggle bits of other in C++
- Clear/Set a specific bit of a number in Arduino
- What is Least significant bit algorithm in Information Security?
- Find position of left most dis-similar bit for two numbers in C++
- Position of rightmost set bit in C++
- Queries for number of array elements in a range with Kth Bit Set using C++
- Python Program to Clear the Rightmost Set Bit of a Number
- 8085 program to find the set bit of accumulator
- 8085 program to find square of a 8 bit number
- Find all divisors of a natural number - Set 1 in C++
- Find all divisors of a natural number - Set 2 in C++
- How do you set, clear, and toggle a bit in C/C++?

Advertisements