
- 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
Find a value whose XOR with given number is maximum in C++
In this tutorial, we are going to write a program that finds the number whose XOR operation with the given number is maximum.
We are assuming the number of bits here is 8.
The XOR operation of different bits gives you the 1 bit. And the XOR operation between the same bits gives you the 0 bit.
If we find the 1's complement of the given number, then that's the number we are looking for.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; int findNumberWithMaximumXOR(int X) { return ((1 << 8) - 1) ^ X; } int main() { int X = 4; cout << findNumberWithMaximumXOR(X) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
251
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- C++ Program to find Number Whose XOR Sum with Given Array is a Given Number k
- Maximum XOR value in matrix in C++
- Find maximum XOR of given integer in a stream of integers in C++
- Find the Number of Unique Triplets Whose XOR is Zero using C++
- Find Maximum XOR value of a sub-array of size k in C++
- Count smaller numbers whose XOR with n produces greater value in C++
- Maximum XOR value of a pair from a range in C++
- Find the node whose absolute difference with X gives maximum value in C++
- C program to find out the maximum value of AND, OR, and XOR operations that are less than a given value
- Find a pair from the given array with maximum nCr value in C++
- Count numbers whose sum with x is equal to XOR with x in C++
- Count numbers whose difference with N is equal to XOR with N in C++
- Count numbers whose XOR with N is equal to OR with N in C++
- Find XOR of two number without using XOR operator in C++

Advertisements