
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Largest set with bitwise OR equal to n in C++
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.
#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.
- Related Articles
- Maximum subset with bitwise OR equal to k in C++
- Find N distinct numbers whose bitwise Or is equal to K in C++
- Largest number smaller than or equal to N divisible by K in C++
- Triples with Bitwise AND Equal To Zero in C++
- Find N distinct numbers whose bitwise Or is equal to K in Python
- Bitwise OR of N binary strings in C++
- Count numbers whose XOR with N is equal to OR with N in C++
- Print bitwise AND set of a number N in C++
- Find largest number smaller than N with same set of digits in C++
- Find the largest number with n set and m unset bits in C++
- Find four factors of N with maximum product and sum equal to N - Set-2 in C++
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Count pairs with Bitwise OR as Even number in C++
- Count pairs with bitwise OR less than Max in C++
- Find subsequences with maximum Bitwise AND and Bitwise OR in Python

Advertisements