Check if all bits of a number are set

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
```

Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

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 loop terminates without returning ‘No’, then all bits are set in the binary representation of n.

C++

 `// C++ implementation to check whether every ` `// digit in the binary representation of the ` `// given number is set or not ` `#include ` `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 above ` `int` `main() ` `{ ` `    ``int` `n = 7; ` `    ``cout << areAllBitsSet(n); ` `    ``return` `0; ` `} `

Java

 `// java implementation to check  ` `// whether every digit in the  ` `// binary representation of the ` `// given number is set or not ` `import` `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 `

Python3

 `# 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 ``=` `7` `print``(areAllBitsSet(n)) ` ` `  `# This code is contributed ` `# by Anant Agarwal. `

C#

 `// C# implementation to check  ` `// whether every digit in the  ` `// binary representation of the ` `// given number is set or not ` `using` `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

 ` 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 ` `?> `

Output :

```Yes
```

Time Complexity : O(d), where ‘d’ is the number of bits in the binary representation of n.

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 which will be a perfect power of 2. Now, check whether the new number is a perfect power of 2 or not.

C++

 `// C++ implementation to check whether every ` `// digit in the binary representation of the ` `// given number is set or not ` `#include ` `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 above ` `int` `main() ` `{ ` `    ``int` `n = 7; ` `    ``cout << areAllBitsSet(n); ` `    ``return` `0; ` `} `

Java

 `// JAVA implementation to check whether  ` `// every digit in the binary representation  ` `// of the given number is set or not ` `import` `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 `

Python3

 `# 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 ``=` `7` `print``(areAllBitsSet(n)) ` ` `  `# This code is contributed ` `# by Anant Agarwal. `

C#

 `// C# implementation to check  ` `// whether every digit in the  ` `// binary representation of  ` `// the given number is set or not ` `using` `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

 ` `

Output :

```Yes
```

This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Improved By : jit_t

Article Tags :
Practice Tags :

6

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.