# How to check if a number is power of 2 or not in C++ (different methods)?

In this article, we are going t learn **how to check, whether a given number is power of 2 or not using C++ program?** Here, we are using different 4 methods to check it.

Submitted by Shubham Singh Rajawat, on February 27, 2018

**Suppose if a number N is given and you have to find out if N is power of 2 or not.**

**There are many solutions to this problem**

**1) By simply repeatedly diving N by 2 if N is even number. If it end up at 1 then N is power of 2**

#include <iostream> using namespace std; int main() { int n; cout<<"Enter the number :"; cin>>n; if(n>0) { while(n%2 == 0) { n/=2; } if(n == 1) { cout<<"Number is power of 2"<<endl; } } if(n == 0 || n != 1) { cout<<"Number is not power of 2"<<endl; } return 0; }

**Output**

First run: Enter the number :10 Number is not power of 2 Second run: Enter the number :16 Number is power of 2

**2) By taking log2 of N and then pass it to floor and ceil if both gives same result then N is power of 2**

#include <iostream> #include <cmath> using namespace std; int main() { int n; cout<<"Enter the number :"; cin>>n; if(ceil(log2(n))== floor(log2(n))) { cout<<"Number is power of 2"<<endl; } else { cout<<"Number is not power of 2"<<endl; } }

**Output**

First run: Enter the number :10 Number is not power of 2 Second run: Enter the number :16 Number is power of 2

**3) By using bit manipulation**

Suppose N = 8 = (1000)_{2}Then N-1 = 7 = (0111)_{2}N & (N-1)= (1000)_{2}& (0111)_{2}= (0000)_{2}N = 5 = (0101)_{2}N-1 = 4 = (0100)_{2}N & (N-1) = (0101)_{2}& (0100)_{2}= (0001)_{2}

If a number is power of 2 then in binary representation the count of 1 will be one.

#include <iostream> using namespace std; int main() { int n; cout<<"Enter the number :"; cin>>n; if(n != 0 && (n & (n-1)) == 0) { cout<<"Number is power of 2"<<endl; } else { cout<<"Number is not power of 2"<<endl; } }

**Output**

First run: Enter the number :10 Number is not power of 2 Second run: Enter the number :16 Number is power of 2

**4) By counting the number of 1’s in the binary form of N. If count is 1 then N is power of 2.**

#include <iostream> using namespace std; int main() { int n,count1=0; cout<<"Enter the number :"; cin>>n; while(n) { n = n & (n-1); count1++; } if(count1 == 1) { cout<<"Number is power of 2"<<endl; } else { cout<<"Number is not power of 2"<<endl; } }

**Output**

**Reference** -Program to find whether a no is power of two

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.