C++ Program to check whether a given number is power of two

In this blog post, I will teach you how to write C++ Program to check whether a given number is power of two. After reading this post you able to check whether given number is power of two or not.

But before writing the CPP program to how to check given number is power of 2 first let’s understand power of two.

An integer n is a power of two, if there exists an integer x such that n == 2x

For example,

Example 1:

Input: n = 8
Output: Yes
Explanation: 23 = 8

Example 2:

Input: n = 32
Output: YES
Explanation: 25 = 32

Example 3:

Input: n = 9
Output: No

 

Let’s see the problem statements and constraints for this question.

Problem Statement: Given an integer n, return 1 if it is a power of two. Otherwise, return 0.

Constraints:

➤  -231 <= n <= 231 -1

 

Disclaimer: Try to solve the problem yourself first otherwise it would be not worth solving this problem.

 

C++ Program to check whether a given number is power of two or not:

/**
 C++ Program to find whether a given number
 is power of two.
*/

#include <iostream>

bool checkPowerOfTwo(int num)
{
    /* Zero and negative numbers
    are not powers of two*/
    return ((num >! 0) && (!(num & (num - 1))));
}

int main()
{
    int num;
    printf("Enter a number: ");
    std::cin>> num;

    const bool isPowerOfTwo = checkPowerOfTwo(num);
    if (isPowerOfTwo)
    {
        std::cout<< num<<" is a power of two";
    }
    else
    {

        std::cout<< num<<" is not a power of two";
    }

    return 0;
}

Output1:

Enter a number: 8
8 is a power of two.

Output2:

Enter a number: 5
5 is not a power of two.

Explanation:

Reason of (num -1):

When 1 is subtracted from every power of 2 number then MSB bit flips to 0 and all preceding bits flip to 1.  For example,

Consider num = 8 (binary representation: 1000). Subtracting 1 gives 7 (binary: 0111).

Reason of num & (num -1):

Arithmetic operation (num -1) produces a number which inverse to the num (if num is power of 2). So, when AND-ing num and (num-1), you will get 0 as the result. That means if the result of this operation is 0, it implies that num has only one bit set to 1 (i.e., it is a power of two).

Let’s understand the operation in detail with below expression.

                    n = 8
decimal |   8 = 2*2*2   |  8 - 1 = 7   |   8 & 7 = 0
        |          ^   |              |
binary  |   1 0 0 0    |   0 1 1 1    |    1 0 0 0
        |   ^          |              |  & 0 1 1 1
index   |   3 2 1 0    |              |    -------
                                           0 0 0 0




-----------------------------------------------------



                    n = 5
decimal | 5 = 2*2 + 1 |  5 - 1 = 4   |   5 & 4 = 4
        |              |              |
binary  |    1 0 1     |    1 0 0     |    1 0 1
        |              |              |  & 1 0 0
index   |    2 1 0     |              |    ------
                                           1 0 0

 

 

Recommended Articles for you: