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.