- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to find maximum possible value for which XORed sum is maximum
Suppose we have two numbers a and b. We have to find the smallest possible value of (a XOR x) + (b XOR x) for some value of x.
So, if the input is like a = 6; b = 12, then the output will be 10, because if x = 4, then (6 XOR 4) + (12 XOR 4) = 2 + 8 = 10.
Steps
To solve this, we will follow these steps −
return a XOR b
Example
Let us see the following implementation to get better understanding −
#include<bits/stdc++.h> using namespace std; int solve(int a, int b){ return (a^b); } int main(){ int a = 6; int b = 12; cout << solve(a, b) << endl; }
Input
6, 12
Output
10
Advertisements