Check if all bits of a number are set (original) (raw)

Given a number n, check whether every bit in the binary representation of the given number is setor not, if yes return trueelse returnfalse.

**Examples :

**Input: n = 7
**Output: true
**Explanation: Binary for 7 is 111, all the bits are set so output is true.

**Input: n = 8
**Output: false
**Explanation: Binary for 8 is 1000, all the bits are not set so output is false.

Table of Content

[Naive Approach] Bits by Bit Traversal - O(log n) Time and O(1) Space

The intuition is to check every bit one by one starting from the right. If any bit is 0 (not set), return false immediately. If you get through the whole number and every bit was 1, return true.

Consider the following dry run: n = 7, binary representation of 7 : 111

For n = 7 (111), (n & 1) = 1 ---> OK, right shift by 1 ---> n = 3 (011)
For n = 3(011), (n & 1) = 1 ---> OK, right shift by 1 ---> n = 1 (001)
For n = 1(001), (n & 1) = 1 ---> OK, right shift by 1 ---> n = 0(000)

Loop ends because n = 0, and no 0 bit encountered so returns true.

Final answer : true

C++ `

#include using namespace std;

// Function to check if all the bits are set in the binary representation bool isBitSet(int n) { // If n is 0, no bits are set if (n == 0) return false;

// Loop until n becomes 0
while (n > 0)
{
    // Check if the last bit is 0
    if ((n & 1) == 0)
    {
        return false;
    }

    // Right shift n by 1
    n = n >> 1;
}

// All bits are 1
return true;

}

int main() { int n = 7; cout << boolalpha << isBitSet(n); return 0; }

Java

class GFG {

// Function to check if all bits are set
static boolean isBitSet(int n)
{
    // If n is 0, no bits are set
    if (n == 0)
        return false;

    // Loop until n becomes 0
    while (n > 0) {

        // Check if last bit is 0
        if ((n & 1) == 0) {
            return false;
        }

        // Right shift n
        n = n >> 1;
    }

    return true;
}

public static void main(String[] args)
{
    int n = 7;
    System.out.println(isBitSet(n));
}

}

Python

def isBitSet(n): # If n is 0, no bits are set if n == 0: return False

# Loop until n becomes 0
while n > 0:
    # Check if last bit is 0
    if (n & 1) == 0:
        return False

    # Right shift n
    n = n >> 1

return True

Driver code

if name == "main": n = 7 print(isBitSet(n))

C#

using System;

class GFG {

// Function to check if all bits are set
static bool isBitSet(int n)
{
    // If n is 0, no bits are set
    if (n == 0)
        return false;

    // Loop until n becomes 0
    while (n > 0) {

        // Check if last bit is 0
        if ((n & 1) == 0) {
            return false;
        }

        // Right shift n
        n = n >> 1;
    }

    return true;
}

public static void Main()
{
    int n = 7;
    Console.WriteLine(isBitSet(n));
}

}

JavaScript

function isBitSet(n) { // If n is 0, no bits are set if (n === 0) return false;

// Loop until n becomes 0
while (n > 0) {
    // Check if last bit is 0
    if ((n & 1) === 0) {
        return false;
    }

    // Right shift n
    n = n >> 1;
}

return true;

}

// Driver code let n = 7; console.log(isBitSet(n));

`

[Alternate Approach] Check for (2^k − 1) - O(log n) Time and O(1) Space

The idea is that a number whose binary representation has all bits set (like 7 → 111, 15 → 1111) is always of the form 2^k - 1, where k is the number of set bits. So, we first count the number of set bits in the number. Let this count be k. Then we construct the number (2^k - 1), which is a number having exactly k bits set (like 111...k times). If the original number n is equal to (2^k - 1), it means all its bits are set. Otherwise, it is not.

Consider the following dry run : n = 7 ---> (111)

Step 1: n = 7 ---> n == 0? NO ---> continue
Step 2: Count set bits in the number n, (since 111 has 3 set bits) so count = 3
Step 3: (1 << count) = (1 << 3) = 8 ---> (1000)
Step 4: (1 << count) - 1 = 8 - 1 = 7 ---> (111)
Step 5: Compare n == 7 ---> TRUE
Step 6: return true

Final answer : true

C++ `

#include using namespace std;

bool isBitSet(int n) { if (n == 0) return false;

int count = __builtin_popcount(n);

// Check if number is of form (2^k - 1)
return n == ((1 << count) - 1);

}

int main() { int n = 7;

cout << boolalpha << isBitSet(n);

return 0;

}

Java

class GFG {

// Function to check if all bits are set
static boolean isBitSet(int n)
{
    // If n is 0, no bits are set
    if (n == 0)
        return false;

    // Loop until n becomes 0
    while (n > 0) {

        // Check if last bit is 0
        if ((n & 1) == 0) {
            return false;
        }

        // Right shift n
        n = n >> 1;
    }

    return true;
}

public static void main(String[] args)
{
    int n = 7;
    System.out.println(isBitSet(n));
}

}

Python

def isBitSet(n): # If n is 0, no bits are set if n == 0: return False

# Loop until n becomes 0
while n > 0:
    # Check if last bit is 0
    if (n & 1) == 0:
        return False

    # Right shift n
    n = n >> 1

return True

Driver code

if name == "main": n = 7 print(isBitSet(n))

C#

using System;

class GFG {

// Function to check if all bits are set
static bool isBitSet(int n)
{
    // If n is 0, no bits are set
    if (n == 0)
        return false;

    // Loop until n becomes 0
    while (n > 0) {

        // Check if last bit is 0
        if ((n & 1) == 0) {
            return false;
        }

        // Right shift n
        n = n >> 1;
    }

    return true;
}

public static void Main()
{
    int n = 7;
    Console.WriteLine(isBitSet(n));
}

}

JavaScript

function isBitSet(n) { // If n is 0, no bits are set if (n === 0) return false;

// Loop until n becomes 0
while (n > 0) {
    // Check if last bit is 0
    if ((n & 1) === 0) {
        return false;
    }

    // Right shift n
    n = n >> 1;
}

return true;

}

// Driver code let n = 7; console.log(isBitSet(n));

`

[Optimal Approach] Using Check for Power of Two - O(1) Time and O(1) Space

The idea is that if a number n has all bits set in its binary representation (like 7 → 111), then adding 1 to it will produce a power of two (like 8 → 1000).

This happens because adding 1 to a sequence of all 1s causes a carry to propagate through all bits, turning them into 0s and adding a new leading 1. So, we compute x = n + 1 and then check whether x is a power of 2.

To check if a number is a power of 2, we use the condition (x & (x - 1)) == 0, which works because powers of 2 have exactly one bit set in binary.

Consider the following dry run : n = 7 --> (111)

Step 1: n = 7 ---> n <= 0? NO ---> continue
Step 2: x = n + 1 = 7 + 1 = 8 ---> (1000)
Step 3: x - 1 = 8 - 1 = 7 ---> (0111)
Step 4: (x & (x - 1)) = 1000 & 0111 = 0000 ---> equals 0
Step 5: return true

Final answer : true

C++ `

#include using namespace std;

bool isBitSet(int n) { // 0 has no bits set if (n <= 0) return false;

int x = n + 1;

// Check if x is power of 2
return (x & (x - 1)) == 0;

}

int main() { int n = 7; cout << boolalpha << isBitSet(n);

return 0;

}

Java

class GFG {

static boolean isBitSet(int n)
{
    // 0 or negative numbers
    if (n <= 0)
        return false;

    int x = n + 1;

    // Check if x is power of 2
    return (x & (x - 1)) == 0;
}

public static void main(String[] args)
{
    int n = 7;
    System.out.println(isBitSet(n));
}

}

Python

def isBitSet(n): # 0 or negative numbers if n <= 0: return False

x = n + 1

# Check if x is power of 2
return (x & (x - 1)) == 0

Driver Code

if name == "main": n = 7 print(isBitSet(n))

C#

using System;

class GFG {

static bool isBitSet(int n)
{
    // 0 or negative numbers
    if (n <= 0)
        return false;

    int x = n + 1;

    // Check if x is power of 2
    return (x & (x - 1)) == 0;
}

public static void Main()
{
    int n = 7;
    Console.WriteLine(isBitSet(n));
}

}

JavaScript

function isBitSet(n) { // 0 or negative numbers if (n <= 0) return false;

let x = n + 1;

// Check if x is power of 2
return (x & (x - 1)) === 0;

}

// Driver code let n = 7; console.log(isBitSet(n));

`