Check if all bits of a number are set in Python

In computers, data is stored in the binary form, which uses the two digits 0 and 1. Each digit in this format is known as a bit.

In this article, we are going to check if all bits of a number are set in Python. A bit is said to be set if the binary representation of every bit is 1.

Let's look at some scenarios to understand this better:

Scenario 1

Input: 3
Binary: 11
Output: True
Explanation: All bits are 1.

Scenario 2

Input: 6
Binary: 110
Output: False
Explanation: Not all bits are 1.

Understanding the Pattern

Numbers with all bits set follow a mathematical pattern. Such numbers are always of the form 2n - 1, where n is a positive integer.

This is because subtracting 1 from a power of 2 results in a binary number where all the lower n bits are set to 1. For example:

2^1 - 1 = 1 ? Binary: 1
2^2 - 1 = 3 ? Binary: 11
2^3 - 1 = 7 ? Binary: 111
2^4 - 1 = 15 ? Binary: 1111

Method 1: Using Bitwise AND Operation

The most efficient way uses the mathematical property that for numbers with all bits set, (n + 1) & n equals zero:

def all_bits_set(n):
    return n != 0 and ((n + 1) & n) == 0

# Test with different numbers
test_numbers = [1, 3, 6, 7, 15, 16]

for num in test_numbers:
    result = all_bits_set(num)
    print(f"Input: {num}, Binary: {bin(num)[2:]}, All Bits Set: {result}")
Input: 1, Binary: 1, All Bits Set: True
Input: 3, Binary: 11, All Bits Set: True
Input: 6, Binary: 110, All Bits Set: False
Input: 7, Binary: 111, All Bits Set: True
Input: 15, Binary: 1111, All Bits Set: True
Input: 16, Binary: 10000, All Bits Set: False

Method 2: Using Mathematical Formula

We can also check if a number equals 2^n - 1 by verifying if n + 1 is a power of 2:

import math

def all_bits_set_formula(n):
    if n <= 0:
        return False
    
    # Check if n+1 is a power of 2
    return (n + 1) & n == 0

# Alternative approach using logarithm
def all_bits_set_log(n):
    if n <= 0:
        return False
    
    # If n = 2^k - 1, then n+1 = 2^k
    # log2(n+1) should be an integer
    log_val = math.log2(n + 1)
    return log_val == int(log_val)

# Test both methods
test_numbers = [1, 3, 7, 15, 31, 32]

print("Method 1 (Bitwise):")
for num in test_numbers:
    result = all_bits_set_formula(num)
    print(f"{num}: {result}")

print("\nMethod 2 (Logarithm):")
for num in test_numbers:
    result = all_bits_set_log(num)
    print(f"{num}: {result}")
Method 1 (Bitwise):
1: True
3: True
7: True
15: True
31: True
32: False

Method 2 (Logarithm):
1: True
3: True
7: True
15: True
31: True
32: False

How It Works

The bitwise method works because when all bits are set (like 111 for 7), adding 1 creates a number with a single bit set in a higher position (1000 for 8). The AND operation between these two patterns always results in zero.

For example:

  • 7 (111) + 1 = 8 (1000)
  • 111 & 1000 = 0

Comparison

Method Time Complexity Space Complexity Best For
Bitwise AND O(1) O(1) Most efficient
Logarithm O(1) O(1) Mathematical verification

Conclusion

To check if all bits of a number are set in Python, use the bitwise trick (n + 1) & n == 0. This works for numbers like 1, 3, 7, 15, 31, etc., where all bits in their binary representation are 1.

Updated on: 2026-03-25T14:13:05+05:30

591 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements