Home » C++ programs

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

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

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

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




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.