# Check if all bits of a number are set in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a number n. We have to check whether all bits are set (1) for the given number n or not.

So, if the input is like n = 255, then the output will be True as the binary representation of 255 is 11111111.

To solve this, we will follow these steps −

• if number is same as 0, then
• return False
• while number > 0, do
• if number is even, then
• return False
• number := quotient of (number / 2)
• return True

Let us see the following implementation to get better understanding −

## Example

Live Demo

def solve(number):
if number == 0:
return False
while number > 0:
if (number & 1) == 0:
return False
number = number >> 1
return True
n = 255
print(solve(n))

## Input

255

## Output

True