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

PythonServer Side ProgrammingProgramming

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
Published on 29-Dec-2020 13:42:19