# Power of two python – Python Program to Find Whether a Number is a Power of Two

Power of two python: In this tutorial, the power of 2 or not for python programming can be checked. The user gives a positive integer N and we have to check if it is equivalent to 2^x or not, where x can be zero or a number positive.

Examples:

Example1:

Input:

given number =2048

Output:

The given numb 2048 is power of 2

Example2:

Input:

given number =256

Output:

The given numb 256 is power of 2

Example3:

Input:

given number =678

Output:

The given numb 678 is not power of 2

## Program to Find Whether a Number is a Power of Two in Python

There are several ways to check whether the given number is a power of 2 or not some of them are:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

### Method #1:Using log function

• This answer is just a mathematical one.
• If the log2(n) of the given number n is integer than it is power of 2
• Else it is not power of 2.

Below is the implementation:

# importing log from math
from math import log
# given number
numb = 2048
# given base (here it is 2 as we have to check whether it is power of 2 or not)
bas = 2
# calculating log2 of the  given numb using log function
logvalue = log(numb, bas)
# if the value is integer then it is power of 2
if (int(logvalue) == logvalue):
print("The given numb", numb, "is power of 2")
# else it is not power of 2
else:
print("The given numb", numb, "is not power of 2")


Output:

The given numb 2048 is power of 2

### Method #2:Using while loop

Continue dividing the number into two, i.e. do n = n/2 as long as n is 1 If n percent 2 is non-zero and n is not 1 in all iteration, then n is not 2. It is a power of 2, if n becomes 1.

Below is the implementation:

# function which returns true if the given number is power of 2 else it reeturns false

def checktwoPower(numb):
# checking if the number is 0
if (numb == 0):
# if the number is 0 then it is not power of 2 hence return false
return False
# using while loop to iterate till the number not equal to 1
while (numb != 1):
# if the bit is set bit then it is not power of 2 so return False
if (numb % 2 != 0):
return False
# dividing n
numb = numb // 2
return True

# given number
numb = 2048
# passing the given number to checktwoPower function
# if the value is integer then it is power of 2
if (checktwoPower(numb)):
print("The given numb", numb, "is power of 2")
# else it is not power of 2
else:
print("The given numb", numb, "is not power of 2")


Output:

The given numb 2048 is power of 2

### Method #3:Using Bitwise Operators

If the number is 2 power, the binary representation of only 1 bit is set.
If we remove 1 from a power number 2, then all the bits are set and the set bit is unset after the set bits (just one bits are set according to point-1)

Therefore, if a number is 2, then

numb&(numb-1) == 0

Only if N is zero is the exception to the foregoing rule. As a power of two, it will give zero which in fact is not. Therefore, we can explicitly see this in the check.

Below is the implementation:

# function which returns true if the given number is power of 2 else it reeturns false

def checktwoPower(numb):
return numb and (not(numb & (numb-1)))

# given number
numb = 2048
# passing the given number to checktwoPower function
# if the value is integer then it is power of 2
if (checktwoPower(numb)):
print("The given numb", numb, "is power of 2")
# else it is not power of 2
else:
print("The given numb", numb, "is not power of 2")


Output:

The given numb 2048 is power of 2

### Method #4:By Calculating total number of set bits

A simple method is to take each bit into consideration in a number (set or unset) and hold a counter to track the set bits if the total number of set bits is 1 then the number is power of 2 else it is not the power of 2

Approach:

• Set the variable to say count to 0 to count the total number of set bits.
• We utilize the while loop.
• We’ll keep going till the number is bigger than zero (Condition of while statement)
• Using the & operator, we will determine whether the last check bit is set or not.
• If the check bit is 1, it indicates that the bit is set, and we increment the count.
• Divide the given number by 2.
• If the count is 1 then it is the power of 2 hence return true
• Else return false

We use n&1 to check whether it is set bit or not.

Below is the implementation:

# function which returns true if the given number is power of 2 else it reeturns false

def checktwoPower(numb):
# checking if the given number is greater than 1
if numb > 1:
# Set the variable say setbitcount to 0 to count the total number of set bits.
setbitcount = 0
# looping till number greater than 0 using while loop
while(numb > 0):
# We will get the last check bit whether it is set bit or not using & operator
# checking if the check bit is 1 or not
# if the check bit is 1 then increment the setbitcount
if(numb & 1):
setbitcount = setbitcount+1
# divide the number by 2
numb = numb//2
# checking if the total number of setbits are 1 or not
return (setbitcount == 1)

# given number
numb = 2048
# passing the given number to checktwoPower function
# if the value is integer then it is power of 2
if (checktwoPower(numb)):
print("The given numb", numb, "is power of 2")
# else it is not power of 2
else:
print("The given numb", numb, "is not power of 2")


Output:

The given numb 2048 is power of 2

Related Programs: