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++
#include <bits/stdc++.h>
using namespace std;
string areAllBitsSet( int n)
{
if (n == 0)
return "No" ;
while (n > 0) {
if ((n & 1) == 0)
return "No" ;
n = n >> 1;
}
return "Yes" ;
}
int main()
{
int n = 7;
cout << areAllBitsSet(n);
return 0;
}
|
C
#include <stdio.h>
void areAllBitsSet( int n)
{
if (n == 0)
printf ( "No" );
while (n > 0) {
if ((n & 1) == 0)
printf ( "No" );
n = n >> 1;
}
printf ( "Yes" );
}
int main()
{
int n = 7;
areAllBitsSet(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static String areAllBitsSet( int n)
{
if (n == 0 )
return "No" ;
while (n > 0 )
{
if ((n & 1 ) == 0 )
return "No" ;
n = n >> 1 ;
}
return "Yes" ;
}
public static void main (String[] args) {
int n = 7 ;
System.out.println(areAllBitsSet(n));
}
}
|
Python3
def areAllBitsSet(n):
if (n = = 0 ):
return "No"
while (n > 0 ):
if ((n & 1 ) = = 0 ):
return "No"
n = n >> 1
return "Yes"
n = 7
print (areAllBitsSet(n))
|
C#
using System;
class GFG
{
static String areAllBitsSet( int n)
{
if (n == 0)
return "No" ;
while (n > 0)
{
if ((n & 1) == 0)
return "No" ;
n = n >> 1;
}
return "Yes" ;
}
static public void Main ()
{
int n = 7;
Console.WriteLine(areAllBitsSet(n));
}
}
|
PHP
<?php
function areAllBitsSet( $n )
{
if ( $n == 0)
return "No" ;
while ( $n > 0)
{
if (( $n & 1) == 0)
return "No" ;
$n = $n >> 1;
}
return "Yes" ;
}
$n = 7;
echo areAllBitsSet( $n );
?>
|
Javascript
<script>
function areAllBitsSet(n)
{
if (n == 0)
return "No" ;
while (n > 0)
{
if ((n & 1) == 0)
return "No" ;
n = n >> 1;
}
return "Yes" ;
}
var n = 7;
document.write(areAllBitsSet(n));
</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++
#include <bits/stdc++.h>
using namespace std;
string areAllBitsSet( int n)
{
if (n == 0)
return "No" ;
if (((n + 1) & n) == 0)
return "Yes" ;
return "No" ;
}
int main()
{
int n = 7;
cout << areAllBitsSet(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static String areAllBitsSet( int n)
{
if (n == 0 )
return "No" ;
if (((n + 1 ) & n) == 0 )
return "Yes" ;
return "No" ;
}
public static void main (String[] args) {
int n = 7 ;
System.out.println(areAllBitsSet(n));
}
}
|
Python3
def areAllBitsSet(n):
if (n = = 0 ):
return "No"
if (((n + 1 ) & n) = = 0 ):
return "Yes"
return "No"
n = 7
print (areAllBitsSet(n))
|
C#
using System;
class GFG
{
static String areAllBitsSet( int n)
{
if (n == 0)
return "No" ;
if (((n + 1) & n) == 0)
return "Yes" ;
return "No" ;
}
static public void Main ()
{
int n = 7;
Console.WriteLine(areAllBitsSet(n));
}
}
|
PHP
<?php
function areAllBitsSet( $n )
{
if ( $n == 0)
return "No" ;
if ((( $n + 1) & $n ) == 0)
return "Yes" ;
return "No" ;
}
$n = 7;
echo areAllBitsSet( $n );
?>
|
Javascript
<script>
function areAllBitsSet(n)
{
if (n == 0)
return "No" ;
if (((n + 1) & n) == 0)
return "Yes" ;
return "No" ;
}
var n = 7;
document.write(areAllBitsSet(n));
</script>
|
Time Complexity: O(d), where ‘d’ is the number of bits in the binary representation of n.
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++
#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;
}
|
Java
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);
}
}
|
Python3
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);
|
C#
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);
}
}
|
Javascript
<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);
</script>
|
Output:
Yes
Time Complexity: O(d), where ‘d’ is the number of bits in the binary representation of n.
Auxiliary Space: O(1)
References:
https://www.careercup.com/question?id=9503107
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Feb, 2023
Like Article
Save Article