# Largest set with bitwise OR equal to n in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this tutorial, we are going to write a program that finds the largest set with bitwise OR is equal to the given number n.

Let's see the steps to solve the problem.

• Initialise the number n.
• Write a loop that iterates from 0 to n.
• If the i | n is equal to n, then add i to the result.
• Return the result.

## Example

Let's see the code.

Live Demo

#include <bits/stdc++.h>
using namespace std;
void printBitWiseOrSet(int n) {
vector<int> v;
for (int i = 0; i <= n; i++) {
if ((i | n) == n) {
v.push_back(i);
}
}
for (int i = 0; i < v.size(); i++) {
cout << v[i] << ' ';
}
cout << endl;
}
int main() {
int n = 7;
printBitWiseOrSet(n);
return 0;
}

## Output

If you run the above code, then you will get the following result.

0 1 2 3 4 5 6 7

## Conclusion

If you have any queries in the tutorial, mention them in the comment section.