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 = 5
Output : 0, 1, 4, 5
Explanation: 0 & 5 = 0
1 & 5 = 1
2 & 5 = 0
3 & 5 = 1
4 & 5 = 4
5 & 5 = 5
So we get 0, 1, 4 and 5 in the
bitwise subsets of N.
Input : N = 9
Output : 0, 1, 8, 9
Simple Approach: A naive approach is to iterate from all numbers from 0 to N and check if (N&i == i). Print the numbers which satisfy the specified condition.
Below is the implementation of above idea:
C++
// CPP program to print all bitwise // subsets of N (Naive approach) #include <bits/stdc++.h> using namespace std; // function to find bitwise subsets // Naive approach void printSubsets(int n) { for (int i = 0; i <= n; i++) if ((n & i) == i) cout << i << " "; } // Driver Code int main() { int n = 9; printSubsets(n); return 0; } |
Java
// JAVA program to print all bitwise // subsets of N (Naive approach) class GFG { // function to find bitwise subsets // Naive approach static void printSubsets(int n) { for (int i = 0; i <= n; i++) if ((n & i) == i) System.out.print(i + " "); } // Driver function public static void main(String[] args) { int n = 9; printSubsets(n); } } // This code is contributed by Anant Agarwal. |
Python3
# Python program to print all bitwise # subsets of N (Naive approach) def printSubsets(n): for i in range(n + 1): if ((n & i) == i): print(i ," ", end = "") # Driver code n = 9printSubsets(n) # This code is contributed by Anant Agarwal. |
C#
// C# program to print all bitwise // subsets of N (Naive approach) using System; class GFG { // function to find bitwise subsets // Naive approach static void printSubsets(int n) { for (int i = 0; i <= n; i++) if ((n & i) == i) Console.Write(i + " "); } // Driver function public static void Main() { int n = 9; printSubsets(n); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to print all bitwise // subsets of N (Naive approach) // function to find bitwise subsets // Naive approach function printSubsets($n) { for ($i = 0; $i <= $n; $i++) if (($n & $i) == $i) echo $i." "; } // Driver Code $n = 9; printSubsets($n); // This code is contributed by mits ?> |
Output :
0 1 8 9
Time Complexity : O(N)
Efficient Solution: An efficient solution is to use bitwise operators to find the subsets. Instead of iterating for every i, we can simply iterate for the bitwise subsets only. Iterating backward for i=(i-1)&n gives us every bitwise subset, where i starts from n and ends at 1.
Below is the implementation of above idea:
CPP
// CPP program to print all bitwise // subsets of N (Efficient approach) #include <bits/stdc++.h> using namespace std; // fucntion to find bitwise subsets // Efficient approach void printSubsets(int n) { for (int i = n; i > 0; i = (i - 1) & n) cout << i << " "; cout << 0; } // Driver Code int main() { int n = 9; printSubsets(n); return 0; } |
Java
// Java program to print all bitwise // subsets of N (Efficient approach) class GFG { // function to find bitwise // subsets Efficient approach static void printSubsets(int n) { for (int i = n; i > 0; i = (i - 1) & n) System.out.print(i + " "); System.out.print(" 0 "); } // Driver Code public static void main(String[] args) { int n = 9; printSubsets(n); } } // This code is contributed by ajit. |
Python3
# Python 3 program to # print all bitwise # subsets of N # (Efficient approach) # fucntion to find # bitwise subsets # Efficient approach def printSubsets(n): i=n while(i != 0): print(i,end=" ") i=(i - 1) & n print("0") # Driver Code n = 9 printSubsets(n) # This code is contributed by # Smith Dinesh Semwal |
C#
// C# program to print all bitwise // subsets of N (Efficient approach) using System; public class GFG { // fucntion to find bitwise subsets // Efficient approach static void printSubsets(int n) { for (int i = n; i > 0; i = (i - 1) & n) Console.Write(i +" "); Console.WriteLine("0"); } // Driver Code static public void Main () { int n = 9; printSubsets(n); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to print all bitwise // subsets of N (Efficient approach) // function to find bitwise subsets // Efficient approach function printSubsets($n) { for ($i = $n; $i > 0; $i = ($i - 1) & $n) echo $i." "; echo "0"; } // Driver Code $n = 9; printSubsets($n); // This code is contributed by mits ?> |
Output :
9 8 1 0
Time Complexity: O(K), where K is the number of bitwise subsets of N.
Recommended Posts:
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Count pairs with Bitwise XOR as ODD number
- Count pairs with Bitwise-AND as even number
- Count pairs with Bitwise XOR as EVEN number
- Count pairs with Bitwise AND as ODD number
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- Multiply any Number with 4 using Bitwise Operator
- Count pairs with Bitwise OR as Even number
- Number of pairs with Bitwise OR as Odd number
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Check whether bitwise AND of a number with any subset of an array is zero or not
- Check if a number is divisible by 17 using bitwise operators
- Check if a number is divisible by 8 using bitwise operators
- Check if a number is multiple of 9 using bitwise operators
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.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


