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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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 ``=` `9` `printSubsets(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

 ` `

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

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

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 Check out this Author's contributed articles.

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.

Improved By : Mithun Kumar, jit_t

Article Tags :
Practice Tags :

1

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