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:
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:
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:
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:
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:
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.