- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 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