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 = 10 = (1010)_{2}N & (N-1) = (0101)_{2}& (1010)_{2}= (0000)_{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

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions

.resCodeAS1 { display: block; width: 320px; height: 50px; }
@media(min-width: 300px) { .resCodeAS1 { display: none; } }
@media(min-width: 480px) { .resCodeAS1 { display: none; } }
@media(min-width: 750px) { .resCodeAS1 { display: block; width: 336px; height: 280px; } }
(adsbygoogle = window.adsbygoogle || []).push({});

(adsbygoogle = window.adsbygoogle || []).push({});

(adsbygoogle = window.adsbygoogle || []).push({});

solved programs: » C » C++ » DS » Java » C# |

aptitude que. & ans.: » C » C++ » Java » DBMS |

interview que. & ans.: » C » Embedded C » Java » SEO » HR |

close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.