Check if all bits of a number are set

22 Jun, 2022
Given a number n. The problem is to check whether every bit in the binary representation of the given number is set or not. Here 0 <= n.Examples : 
Input : 7
Output : Yes
(7)10 = (111)2

Input : 14
Output : No
Method 1: If n = 0, then answer is ‘No’. Else perform the two operations until n becomes 0. 
While (n > 0)
 If n & 1 == 0, 
     return 'No'
 n >> 1
If the loop terminates without returning ‘No’, then all bits are set in the binary representation of n. 
C++
C
Java
Python3
C#
PHP
Javascript
// C++ implementation to check whether every digit in the// binary representation of the given number is set or not#include <bits/stdc++.h>using namespace std; // function to check if all the bits are set or not in the// binary representation of 'n'string areAllBitsSet(int n){    // all bits are not set    if (n == 0)        return "No";    // loop till n becomes '0'    while (n > 0) {        // if the last bit is not set        if ((n & 1) == 0)            return "No";        // right shift 'n' by 1        n = n >> 1;    }    // all bits are set    return "Yes";} // Driver program to test aboveint main(){    int n = 7;    cout << areAllBitsSet(n);    return 0;} // This code is contributed by Sania Kumari Gupta (kriSania804)
// C implementation to check whether every digit in the// binary representation of the given number is set or not#include <stdio.h> // function to check if all the bits are set or not in the// binary representation of 'n'void areAllBitsSet(int n){    // all bits are not set    if (n == 0)        printf("No");    // loop till n becomes '0'    while (n > 0) {        // if the last bit is not set        if ((n & 1) == 0)            printf("No");        // right shift 'n' by 1        n = n >> 1;    }    // all bits are set    printf("Yes");} // Driver program to test aboveint main(){    int n = 7;    areAllBitsSet(n);    return 0;} // This code is contributed by Sania Kumari Gupta (kriSania804)
// java implementation to check// whether every digit in the// binary representation of the// given number is set or notimport java.io.*; class GFG {         // function to check if all the bits    // are setthe bits are set or not    // in the binary representation of 'n'    static String areAllBitsSet(int n)    {        // all bits are not set        if (n == 0)            return "No";             // loop till n becomes '0'        while (n > 0)        {            // if the last bit is not set            if ((n & 1) == 0)                return "No";                 // right shift 'n' by 1            n = n >> 1;        }                 // all bits are set            return "Yes";    }         // Driver program to test above    public static void main (String[] args) {    int n = 7;         System.out.println(areAllBitsSet(n));    }}  // This code is contributed by vt_m
# Python implementation# to check whether every# digit in the binary# representation of the# given number is set or not # function to check if# all the bits are set# or not in the binary# representation of 'n'def areAllBitsSet(n):     # all bits are not set    if (n == 0):        return "No"      # loop till n becomes '0'    while (n > 0):             # if the last bit is not set        if ((n & 1) == 0):            return "No"          # right shift 'n' by 1        n = n >> 1           # all bits are set    return "Yes"   # Driver program to test above n = 7print(areAllBitsSet(n)) # This code is contributed# by Anant Agarwal.
// C# implementation to check// whether every digit in the// binary representation of the// given number is set or notusing System; class GFG{         // function to check if     // all the bits are set    // or not in the binary    // representation of 'n'    static String areAllBitsSet(int n)    {        // all bits are not set        if (n == 0)            return "No";             // loop till n becomes '0'        while (n > 0)        {            // if the last bit            // is not set            if ((n & 1) == 0)                return "No";                 // right shift 'n' by 1            n = n >> 1;        }                 // all bits are set            return "Yes";    }         // Driver Code    static public void Main ()    {        int n = 7;        Console.WriteLine(areAllBitsSet(n));    }} // This code is contributed by ajit
<?php// PHP implementation to check// whether every digit in the// binary representation of the// given number is set or not // function to check if all the// bits are set or not in the// binary representation of 'n'function areAllBitsSet($n){    // all bits are not set    if ($n == 0)        return "No";     // loop till n becomes '0'    while ($n > 0)    {        // if the last bit is not set        if (($n & 1) == 0)            return "No";         // right shift 'n' by 1        $n = $n >> 1;    }     // all bits are set    return "Yes";} // Driver Code$n = 7;echo areAllBitsSet($n); // This code is contributed by aj_36?>
<script> // javascript implementation to check// whether every digit in the// binary representation of the// given number is set or not    // function to check if all the bits// are setthe bits are set or not// in the binary representation of 'n'function areAllBitsSet(n){    // all bits are not set    if (n == 0)        return "No";     // loop till n becomes '0'    while (n > 0)    {        // if the last bit is not set        if ((n & 1) == 0)            return "No";         // right shift 'n' by 1        n = n >> 1;    }         // all bits are set        return "Yes";}     // Driver program to test abovevar n = 7;document.write(areAllBitsSet(n)); // This code contributed by Princi Singh </script>
Output :  
Yes
Time Complexity: O(d), where ‘d’ is the number of bits in the binary representation of n.Auxiliary Space: O(1) Method 2: If n = 0, then answer is ‘No’. Else add 1 to n. Let it be num = n + 1. If num & (num – 1) == 0, then all bits are set, else all bits are not set. Explanation: If all bits in the binary representation of n are set, then adding ‘1’ to it will produce a number that will be a perfect power of 2. Now, check whether the new number is a perfect power of 2 or not.  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation to check whether every// digit in the binary representation of the// given number is set or not#include <bits/stdc++.h>using namespace std; // function to check if all the bits are set// or not in the binary representation of 'n'string areAllBitsSet(int n){    // all bits are not set    if (n == 0)        return "No";     // if true, then all bits are set    if (((n + 1) & n) == 0)        return "Yes";     // else all bits are not set    return "No";} // Driver program to test aboveint main(){    int n = 7;    cout << areAllBitsSet(n);    return 0;}
// JAVA implementation to check whether// every digit in the binary representation// of the given number is set or notimport java.io.*; class GFG {         // function to check if all the    // bits are set or not in the    // binary representation of 'n'    static String areAllBitsSet(int n)    {        // all bits are not set        if (n == 0)            return "No";             // if true, then all bits are set        if (((n + 1) & n) == 0)            return "Yes";             // else all bits are not set        return "No";    }         // Driver program to test above    public static void main (String[] args) {    int n = 7;    System.out.println(areAllBitsSet(n));    }} // This code is contributed by vt_m
# Python implementation to# check whether every# digit in the binary# representation of the# given number is set or not # function to check if# all the bits are set# or not in the binary# representation of 'n'def areAllBitsSet(n):     # all bits are not set    if (n == 0):        return "No"      # if true, then all bits are set    if (((n + 1) & n) == 0):        return "Yes"      # else all bits are not set    return "No"   # Driver program to test above n = 7print(areAllBitsSet(n)) # This code is contributed# by Anant Agarwal.
// C# implementation to check// whether every digit in the// binary representation of// the given number is set or notusing System; class GFG{         // function to check if all the    // bits are set or not in the    // binary representation of 'n'    static String areAllBitsSet(int n)    {        // all bits are not set        if (n == 0)            return "No";             // if true, then all        // bits are set        if (((n + 1) & n) == 0)            return "Yes";             // else all bits are not set        return "No";    }         // Driver Code    static public void Main ()    {        int n = 7;        Console.WriteLine(areAllBitsSet(n));    }} // This code is contributed by m_kit
<?php// PHP implementation to check// whether every digit in the// binary representation of the// given number is set or not // function to check if all// the bits are set or not in// the binary representation of 'n'function areAllBitsSet($n){    // all bits are not set    if ($n == 0)        return "No";     // if true, then all    // bits are set    if ((($n + 1) & $n) == 0)        return "Yes";     // else all bits    // are not set    return "No";} // Driver Code$n = 7;echo areAllBitsSet($n); // This code is contributed by ajit?>
<script>// javascript implementation to check whether// every digit in the binary representation// of the given number is set or not    // function to check if all the// bits are set or not in the// binary representation of 'n'function areAllBitsSet(n){    // all bits are not set    if (n == 0)        return "No";     // if true, then all bits are set    if (((n + 1) & n) == 0)        return "Yes";     // else all bits are not set    return "No";} // Driver program to test abovevar n = 7;document.write(areAllBitsSet(n)); // This code contributed by Princi Singh</script>
Yes
Time Complexity: O(1)Auxiliary Space:  O(1)
Method 3: We can simply count the total set bits present in the binary representation of the number and based on this, we can check if the number is equal to pow(2, __builtin_popcount(n)). If it happens to be equal, then we return 1, else return 0;
C++
Java
Python3
C#
Javascript
#include <bits/stdc++.h>using namespace std; void isBitSet(int N){    if (N == pow(2, __builtin_popcount(N)) - 1)        cout << "Yes\n";    else cout << "No\n";} int main(){    int N = 7;    isBitSet(N);    return 0;}
import java.util.*; class GFG{ static void isBitSet(int N){    if (N == Math.pow(2, Integer.bitCount(N)) - 1)        System.out.print("Yes\n");    else System.out.print("No\n");} public static void main(String[] args){    int N = 7;    isBitSet(N);}} // This code is contributed by umadevi9616
def bitCount(n):    n = n - ((n >> 1) & 0x55555555);    n = (n & 0x33333333) + ((n >> 2) & 0x33333333);    return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;  def isBitSet(N):    if (N == pow(2, bitCount(N)) - 1):        print("Yes");    else:        print("No");  if __name__ == '__main__':    N = 7;    isBitSet(N);     # This code is contributed by gauravrajput1
using System; public class GFG{     static int bitCount (int n) {          n = n - ((n >> 1) & 0x55555555);          n = (n & 0x33333333) + ((n >> 2) & 0x33333333);          return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;        }           static void isBitSet(int N){    if (N == Math.Pow(2, bitCount(N)) - 1)        Console.Write("Yes\n");    else Console.Write("No\n");} public static void Main(String[] args){    int N = 7;    isBitSet(N);}} // This code is contributed by umadevi9616
<script>   function bitCount (n) {        n = n - ((n >> 1) & 0x55555555);        n = (n & 0x33333333) + ((n >> 2) & 0x33333333);        return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;      }    function isBitSet(N) {        if (N == Math.pow(2, bitCount(N)) - 1)            document.write("Yes\n");        else            document.write("No\n");    }         var N = 7;        isBitSet(N); // This code is contributed by umadevi9616</script>
Output:
Yes
Time Complexity: O(1)Auxiliary Space: O(1)
References: https://www.careercup.com/question?id=9503107This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
princi singh
bhargabnath691
umadevi9616
GauravRajput1
krisania804
ranjanrohit840
Amazon
Bit Magic
Amazon
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.