C++

C++ XOR Operation

C++ programming provides various bitwise operators like AND, OR, NOT, XOR, etc. To operate on the given data at the bit level, we utilize the bitwise operators in the C++ programming language. The “XOR” operator (^) in C++ executes an XOR process on every bit of the two operands. If the two bits are distinct, the outcome of XOR is 1; if both are the same, the outcome is 0. Here, we will study the “XOR” operator in C++ programming.

Example 1:

The code begins here by including the “iostream” header file. As the name suggests, this header file is for the input and output functions as these functions are declared in it. Then, we have the “namespace std” in which these functions are defined.

Below this, we call the “main()” method. We initialize the “x” variable of the type “int” and assign “10” to this “x”. Then, we have another variable, “y”, of the “int” data type and assign “6”. After this, we initialize “r” of the “int” data type. Here, we apply the “XOR” operation on the values of “x” and “y” variables by placing the “^” operator in between these variables. This “XOR” operator converts the integer values into the binary, apply the “XOR” operation on the binary values, and save the result as the integer value. The outcome of this “XOR” operator is now saved in “r”.

After this, we display the values of these variables separately and then display the result that we get after applying the “XOR” operator with the help of “cout”.

Code 1:

#include <iostream>

using namespace std;

int main() {

  int x = 10;

  int y = 6;

  int r = x ^ y;

  cout << "The value of x : " <<x << endl;

  cout << "The value of y : " <<y << endl;

  cout << "The XOR x ^ y = " << r << endl;

  return 0;

}

Output:

Since the binary value of “10” is “1010” and the binary value of “6” is “0110”, it returns “12” after applying the “XOR” operator and “1100” is the binary value of “12”. This shows that it returns “1” where both the inputs are different and returns “0” where both the inputs are the same.

Example 2:

After adding the “iostream” header file and the “std” namespace, we invoke the “main()” method. Then, we initialize two variables, “X1” and “X2”, and assign the “21” and “35” integer values to these variables, respectively. Then, we print both variables’ values. After this, we apply the “XOR” operator to these integer values. We apply this “XOR” operation to these “X1” and “X2” variables inside the “cout”. So, the result of this “XOR” is also displayed as the outcome.

Code 2:

#include <iostream>

using namespace std;

int main() {

  int X1 = 21, X2 = 35;

  cout << "X1 value = " << X1 << endl;

  cout << "X2 value = " << X2 << endl;

  cout << "The XOR result is: " << endl;

  cout << "X1 ^ X2 = " << (X1 ^ X2) << endl;

  return 0;

}

Output:

The first integer value is “21” and the second is “35”. After applying the “XOR” operation, we get the “54” result which is displayed here.

Example 3:

We call the “main()” method after adding the “iostream” header file and the “std” namespace. The “n1” variable of the type “int” is initialized and “29” is assigned to it. Next, we assign “75” to another variable, “n2”, which is of the “int” data type. Next, we initialize the value of “r1″as well as that of the “int” data type.

Next, we apply the “XOR” operation on the values of the “n1” and “n2” variables by placing the “^” operator between them. The integer values are converted to binary using this “XOR” operator which then applies the “XOR” operation to the binary data and save the outcome as an integer value. The “r1” variable now contains the outcome of this “XOR” operation. The values of each of these variables are then shown separately. We also show the outcome of using the “XOR” operator with the assistance of the “cout” operator.

Code 3:

#include <iostream>

using namespace std;

int main()

{

  int n1 = 29;

  int n2 = 75;

  int r1 = n1 ^ n2;

  cout << "The first value : " << n1 << endl;

  cout << "The second value : " << n2 << endl;

  cout << "The outcome of XOR operator is: " << r1 << endl;

  return 0;

}

Output:

The input integers are “29” and “75” which are converted into binary. Then, the “XOR” operation is applied to them. After applying “XOR”, the result is “86”.

Example 4:

In this code, we get the input from the user and then apply the “XOR” operation to the user’s input values. The three variables are declared here with the names “Xvalue1”, “Xvalue2”, and “Xvalue3”. Then, we place the “cout” and display the “Enter two values here” message.

After displaying this message, the user enters the values that we get with the cin’s help. So, we place “cin” below this. Both values are now stored in these variables and are also displayed here. Now, we have to apply the “XOR” operation, so we insert the “^” operator between the “Xvalue1” and “Xvalue2” variables.

Now, this “XOR” operation is applied to the values of these variables. The outcome of this “XOR” operator is then saved in the “Xvalue3” variable. We also display it using the “cout” method.

Code 4:

#include<iostream>

using namespace std;

int main ()

{

  int Xvalue1, Xvalue2, Xvalue3 ;

  cout << "Enter values two values here: " << endl ;

  cout << "Xvalue1: " ;

  cin >> Xvalue1 ;

  cout << "Xvalue2: " ;
 
  cin >> Xvalue2 ;

  Xvalue3 = Xvalue1 ^ Xvalue2 ;

  cout << "\nNow, after applying XOR on both values: "<< endl ;

  cout << "Xvalue1 ^ Xvalue2 = " << Xvalue3 << endl ;

}

Output:

When we execute this code, it prints a message for entering two values. So, we enter “14” as the “Xvalue1” variable’s value and “45” as the value of the “Xvalue2” variable. Then, we hit “Enter”. The “XOR” operation is then applied to these values which converts both values into binary and then displays the result here.

Example 5:

We apply this “XOR” operation to the character data. We initialize two “char” variables with the names “ch_a” and “ch_b”. We assign “a” and “8” to these variables, respectively. Then, we place the “^” operator between “ch_a” and “ch_b” and assigne it to the “ch_result” variable which is also the “char” data type. These characters are converted into binary, and the result is saved in the “ch_result” variable. We then print both variables and the result of this “XOR” operation.

Code 5:

#include <iostream>

using namespace std;

int main() {

  char ch_a = 'a';

  char ch_b = '8';

  char ch_result = ch_a ^ ch_b;

  cout << "The first character is : " << ch_a << endl;

  cout << "The second character is : " << ch_b << endl;

  cout << "The Result is : " << ch_result << endl;

}

Output:

The input characters are “a” and “8” and the result of “XOR” is displayed as “Y” which we get after applying the “XOR” operation that converts “a” and “8” into binary and then performs the “XOR” operation.

Conclusion

The “XOR” operation is explored thoroughly here and we explained that it is the “bitwise” operation as it utilizes the binary values. We discussed that all the values we entered to apply the “XOR” operation are converted into binary values, and then the “XOR” operation is performed. We demonstrated several examples and showed how the “XOR” operation works in C++ programming.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.