Check if all bits of a number are set
Last Updated :
10 Apr, 2025
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++ 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 above
int main()
{
int n = 7;
cout << areAllBitsSet(n);
return 0;
}
// This code is contributed by Sania Kumari Gupta (kriSania804)
C
// 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 above
int main()
{
int n = 7;
areAllBitsSet(n);
return 0;
}
// This code is contributed by Sania Kumari Gupta (kriSania804)
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
Python
# 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
JavaScript
<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 above
var n = 7;
document.write(areAllBitsSet(n));
// This code contributed by Princi Singh
</script>
PHP
<?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
?>
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++
// 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 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
Python
# 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
JavaScript
<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 above
var n = 7;
document.write(areAllBitsSet(n));
// This code contributed by Princi Singh
</script>
PHP
<?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
?>
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++
#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);
}
}
// This code is contributed by umadevi9616
Python
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
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);
}
}
// This code is contributed by umadevi9616
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);
// This code is contributed by umadevi9616
</script>
Output:
Yes
Time Complexity: O(d), where 'd' is the number of bits in the binary representation of n.
Auxiliary Space: O(1)
Similar Reads
Set all even bits of a number
Given a number, the task is to set all even bits of a number. Positions of bits are counted from LSB (least significant bit) to MSB (Most significant bit). The position of LSB is considered as 1. Examples : Input : 20 Output : 30 Binary representation of 20 is 10100. After setting even bits, we get
14 min read
Check if a number has two adjacent set bits
Given a number you have to check whether there is pair of adjacent set bit or not.Examples : Input : N = 67 Output : Yes There is a pair of adjacent set bit The binary representation is 100011 Input : N = 5 Output : No Recommended PracticeNumbers having two adjacent set bitsTry It! A simple solution
3 min read
Set all odd bits of a number
Given a number, the task is to set all odd bits of a number. Positions of bits are counted from LSB (least significant bit) to MSB (Most significant bit). Position of LSB is considered as 1. Examples : Input : 20 Output : 21 Explanation : Binary representation of 20 is 10100. Setting all odd bits ma
15+ min read
Check if a number has bits in alternate pattern | Set 1
Given an integer n > 0, the task is to find whether this integer has an alternate pattern in its bits representation. For example- 5 has an alternate pattern i.e. 101. Print "Yes" if it has an alternate pattern otherwise "No". Here alternate patterns can be like 0101 or 1010.Examples: Input : 15O
7 min read
Set all the bits in given range of a number
Given a non-negative number n and two values l and r. The problem is to set the bits in the range l to r in the binary representation of n, i.e, to unset bits from the rightmost lth bit to the rightmost r-th bit. Constraint: 1 <= l <= r <= number of bits in the binary representation of n.Ex
5 min read
Toggle all even bits of a number
Given a number, the task is to Toggle all even bit of a numberExamples: Input : 10 Output : 0 binary representation 1 0 1 0 after toggle 0 0 0 0 Input : 20 Output : 30 binary representation 1 0 1 0 0 after toggle 1 1 1 1 0 1. First generate a number that contains even position bits. 2. Take XOR with
8 min read
Count unset bits of a number
Given a number n, count unset bits after MSB (Most Significant Bit).Examples : Input : 17 Output : 3 Binary of 17 is 10001 so unset bit is 3 Input : 7 Output : 0 A Simple Solution is to traverse through all bits and count unset bits. C++ // C++ program to count unset bits in an integer #include <
7 min read
Change all even bits in a number to 0
Given a number, change all bits at even positions to 0. Examples: Input : 30 Output : 10 Binary representation of 11110. Bits at Even positions are highlighted. After making all of them 0, we get 01010 Input : 10 Output : 10Recommended PracticeChange all even bits in a number to 0Try It! Method 1 (B
6 min read
Check if a number has same number of set and unset bits
Given a number N, the task is to check whether the count of the set and unset bits in the given number are same. Examples: Input: 12Output: Yes1100 is the binary representation of 12which has 2 set and 2 unset bits Input: 14Output: No Approach: Traverse in the binary representation of the given numb
5 min read
Print bitwise AND set of a number N
Given a number N, print all the numbers which are a bitwise AND set of the binary representation of N. Bitwise AND set of a number N is all possible numbers x smaller than or equal N such that N & i is equal to x for some number i. Examples : Input : N = 5Output : 0, 1, 4, 5 Explanation: 0 &
8 min read