Q:

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

belongs to collection: C++ programs on various topics

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Print character through ASCII value using cout in ... >>
<< Jump Search Implementation using C++...