C++ Bitwise XOR Operator
Exclusive OR is another name for the bitwise XOR operator. The ‘^’ is used to indicate it. It operates at the bit level of the operands, as the name implies. Bitwise XOR operator has come under the domain of Bitwise operators. Two operands are required for the OR special operating system (XOR), and these two operands are separated by an XOR symbol, namely ‘^’. The logical truth table of the XOR operator must be followed to ascertain the output or result obtained after applying the XOR operator to two operands. The reason for the XOR operator is that if the XOR operation is performed on two different servants of two operands, the result will always be '1', but if the XOR operation is performed on two identical pieces of two operands, the result will always be the same be '0'.
Truth Table
Let's say there are two operands: X and Y. The total number of input combinations created by these two operands will be four. The matching output will be determined using the XOR truth table below. C will be used to represent the result, where Z = X ^ Y.
The input to this truth table is in the form of bits, i.e., 0 and 1, and the output will likewise be in the form of bits, i.e., 0 and 1.
The XOR Truth Table is a mathematical table created utilizing the XOR operator's correct logic.
X | Y | Z = X ^ Y |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
In the following XOR Truth table, we can see that when the operands X and Y have different values, such as (0, 1), (1, 0), the result will always be 1. And when the operands X and Y have the same values, such as (0, 0), (1, 1), the result will always be 0.
Similarly, we may build the truth table for Boolean values in similar manner. Let's say there are two operands: one is X and the other is Y. The total number of input combinations created by these two operands will be four. The matching output will be determined using the XOR truth table below. The outcome will be expressed as Z, where Z = X Y. In this truth table, we're taking input in the form of True (T) and False (F) values (F). True values, i.e. T and F, will also be created as part of the output.
X | Y | Z = X ^ Y |
F | F | F |
F | T | T |
T | F | T |
T | T | F |
In the XOR Truth table above, we can see that when the operands X and Y have different values, i.e. (False, True), (True, False), the result is always True. And when the operands X and Y have the identical values, i.e. (False, False), (True, True), the result is always False.
Example:
#include < iostream >
#include < bits/stdc++.h >
#include < stdlib >
#include < stdio >
#include < math >
using namespace std ;
int main ()
{
int a , b , c ; // Initializing integer variables to store data values
cout << "Enter values of a and b -> " << endl ;
cout << "a: " ;
cin >> a ; // taking a as input from user
cout << "b: " ;
cin >> b ; // taking b as input from user
c = a ^ b ; // storing XOR result of a and b in c
cout << "Applying XOR operation on a and b: "<< endl ;
cout << "a ^ b = " << c << endl ; // Printing the output
return 0 ;
}
OUTPUT:
Enter the value of a and b ->
a: 20
b: 30
Applying XOR Operation on a and b
a ^ b = 10
……………………………………………………
Process executed in 3.22 seconds
Press any key to continue.
Explanation:
In the above example of a program in C++ we have demonstrated the bitwise XOR.
Find the exclusive OR of three integer values: three and fifteen. Also, explain the situation and write the C++ code for execution:
#include < bits/stdc++.h >
#include < stdlib >
#include < stdio >
#include < math >
#include< iostream >
using namespace std ;
int main ( )
{
int a , b , c ; // Initializing integer variables to store data values
cout << "Enter values of a and b -> " << endl ;
cout << "a: " ;
cin >> a ; // taking a as input from user
cout << "b: " ;
cin >> b ; // taking b as input from user
c = a ^ b ; // storing XOR result of a and b in c
cout << "Applying XOR operation on a and b: "<< endl ;
cout << "a ^ b = " << c << endl ; // Printing the output
return 0;
}
OUTPUT:
Enter the value of a and b ->
a: 20
b: 10
Applying XOR Operation on a and b
a ^ b = 30
……………………………………………………
Process executed in 3.22 seconds
Press any key to continue.
Explanation:
In the above example of a program in C++ we have demonstrated the bitwise XOR.