
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 position of the only set bit in C++
In this problem we are given a number N which has only one set bit in its binary representation. Our task is to find the position of the only set bit. If the number has only one set bit return the position of the number otherwise print invalid number.
Let’s take an example to understand the problem,
Input
N = 32
Output
6
Explanation
Binary representation of the number is 10000.
Solution Approach
One fact to know before we proceed further is the number will have only 1 set bit if it is a power of 2. Otherwise it will have more number of set bits.
A simple solution is by starting from the rightmost bit and then check the value of bits. We will use a loop to check if it is set or not.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; bool isPowerOfTwo(unsigned n) { if(n>0) { while(n%2 == 0) n/=2; if(n == 1) return true; } if(n == 0 || n != 1) return false; return false; } int findPostionOfSetBit(unsigned n) { unsigned i = 1, position = 1; while (!(i & n)) { i = i << 1; ++position; } return position; } int main(void){ int n = 64; if(!isPowerOfTwo(n)) cout<<"Invalid Number!"; else cout<<"The position of the number "<<n<<" is "<<findPostionOfSetBit(n); return 0; }
Output
The position of the number 64 is 7
An alternate method to solve the problem is by using the shift operation to shift the number to right until it becomes 0. At the end the number of shifts done to reach 0 is the position of the set bit.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; bool isPowerOfTwo(unsigned n) { if(n>0) { while(n%2 == 0) n/=2; if(n == 1) return true; } if(n == 0 || n != 1) return false; return false; } int findPostionOfSetBit(unsigned n) { unsigned position = 0; while (n) { n = n >> 1; ++position; } return position; } int main(void){ int n = 64; if(!isPowerOfTwo(n)) cout<<"Invalid Number!"; else cout<<"The position of the number "<<n<<" is "<<findPostionOfSetBit(n); return 0; }
Output
The position of the number 64 is 7
One more method to solve the problem is using math formulas. We know that,
2i = n, where n is the number and i is the position of the number The values of i here can be found using the formula, i = log2(n)
Program to illustrate the working of our solution,
Example
#include <iostream> #include <math.h> using namespace std; bool isPowerOfTwo(unsigned n) { if(n>0) { while(n%2 == 0) n/=2; if(n == 1) return true; } if(n == 0 || n != 1) return false; return false; } int findPostionOfSetBit(unsigned n) { unsigned position = log2(n) + 1; ; return position; } int main(void){ int n = 64; if(!isPowerOfTwo(n)) cout<<"Invalid Number!"; else cout<<"The position of the number "<<n<<" is "<<findPostionOfSetBit(n); return 0; }
Output
The position of the number 64 is 7
- Related Questions & Answers
- Position of rightmost set bit in C++
- Golang Program to find the position of the rightmost set bit
- Position of the K-th set bit in a number in C++
- Position of rightmost different bit in C++
- Count of numbers having only 1 set bit in the range [0, n] in C++
- Find most significant set bit of a number in C++
- Check whether the two numbers differ at one-bit position only in Python
- Set the bit at a specific position in the BitArray to the specified value in C#?
- Find position of left most dis-similar bit for two numbers in C++
- 8085 program to find the set bit of accumulator
- Find letter's position in Alphabet using Bit operation in C++
- Position of rightmost common bit in two numbers in C++
- Check whether the bit at given position is set or unset in Python
- Get value of the bit at a specific position in BitArray in C#
- Update the bit in the given position or Index of a number using C++