- Bitwise XOR operator is also known as Exclusive OR
- It is denoted by using the '^'
- As the name depicts, it works on the bit level of the operands.
- Bitwise XOR operator has come under the category of Bitwise operators.
- In the bitwise exclusive OR operator (XOR), two operands are required, and these two operands are separated by the XOR symbol, i.e., '^'.
- To determine the output or result that comes out after applying the XOR operator on two operands, we need to follow the Logical truth table of the XOR operator.
- XOR Truth Table is the mathematical table constructed using the proper logic of the XOR operator.
- The logic used behind the XOR operator is; whenever XOR operation is applied on the two different bits of two operands, then the result will always produce '1', and if the XOR operation is applied on the two same bits of two operands then the result produces output '0'.
Truth Table of Exclusive OR (XOR) operator
Let's consider two operands, denoted as A and B. There are a total of 4 possible combinations of inputs resulting from these operands. By referring to the XOR truth table below, we can establish the respective output. The outcome will be stored in C, where C equals the XOR operation between A and B.
In this logical table, input is received as binary digits, specifically 0s and 1s, and the resulting output is also produced in the binary form, i.e., 0s and 1s.
In the provided XOR Truth table above, it is evident that when operands A and B have dissimilar values, such as ( 0, 1 ) or ( 1, 0 ), the output will consistently be 1. Conversely, when operands A and B have identical values, like ( 0, 0 ) or ( 1, 1 ), the output will consistently be 0.
Likewise, we can generate the truth table for Boolean values using the same method.
Let's consider two operands: the initial operand is denoted as A and the subsequent one as B. There will be a total of 4 possible input combinations created by these two operands. We will utilize the XOR truth table below to ascertain the resultant output. The output will be stored in variable C, where C equals the XOR operation between A and B.
In this logic table, inputs are accepted as Truth values, which are represented as True ( T ) and False ( F ). The resulting values will also be displayed as True ( T ) and False ( F ).
Here, in the XOR Truth table above, we can see that when the operands A and B have different values, such as (False, True) or (True, False), the output will always be True. Conversely, when the operands A and B have the same values, like (False, False) or (True, True), the output will always be False.
From the provided tables, it can be noted that the value T (True) is represented by the number 1, while the value F (False) is represented by the number 0.
Steps to solve any given problem -
- The operands given in the problem will always be in the decimal value.
- Firstly, we need to convert the values of operands into binary
- After converting the values of operands in binary numbers, put both operands one over each other.
- Remember that before applying exclusive OR (XOR) operation on them, kindly check the number of digits in them.
- If the count of digits does not match, the extra 0's at the left end of the small operand balances the counts of digits.
- Finally, with the help of the above truth table, apply the XOR operation on the operands one by one, taking one bit at a time for applying the XOR operation.
- At last, the result is produced in the form of output.
- The output is produced will be in binary form, now convert the binary form into decimal form and note down the result value.
Execution of Bitwise Exclusive OR (XOR) operation in C++
Let's delve deeper into the process of executing the XOR operation in C++ by exploring some illustrative examples:
Example 1: Find the exclusive OR of integer values; 10 and 14. Also, explain it and write the code of execution in C++.
Solution: Let's define two variables, ' a ' and ' b ', to hold the respective two values provided in the previous scenario, namely, 10 and 14.
Here, a = 10 and b = 14.
We will follow the below steps to find out the exclusive OR of the given two operands.
- We know that 10 and 14 are in decimal form, and for applying bitwise XOR operation, it is necessary to convert it into binary form.
- Binary form ' a ', i.e., 10 is ' 1010 ' and Binary form of ' b ', i.e., 14 is ' 1110 '.
- Here we observe that the count of binary digits present in a is four and the count of binary digits present in b is also 4; hence the number of binary digits present in both the variables are the same and already balanced, we do not need to add more number of 0's to balance it.
- Now, putting the binary digits present in 'b' down to the binary digits present in 'a'.
- Finally, applying the XOR operation one by one on the corresponding bits matches and note down the output.
- The output generated at last will be in binary form, as the above question given in the decimal form, so we need to convert the result in decimal form.
Explanation:
a = 10 ( In Decimal form )
b = 14 ( In Decimal form )
Now, in order to perform the XOR operation on variables a and b, it is essential to first convert both a and b into binary format.
a = 1010 ( In Binary form )
b = 1110 ( In Binary form )
Now, applying XOR operation on a and b -
a = 1010
Performing a bitwise XOR operation on variable 'a' with the value 1110 stored in variable 'b' results in 0100 in binary format.
The outcome of a raised to the power of b is 0100, displayed in binary format.
Now transforming the outcome into decimal format, which equates to 4.
10 ^ 14 = 4
NOTE: By using the above XOR truth table, the output of corresponding bits are generated.
We are going to perform the bitwise exclusive OR operation on the numbers 10 and 14 using the C++ programming language to yield the outcome, which is 4.
C++ code for above example:
//************************ C++ Code ******************************
#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
}
Output
Example 2: Find the exclusive OR of integer values; 3 and 15. Also, explain it and write the code of execution in C++.
Let's define two variables, 'a' and 'b', to hold the respective operands provided in the previous scenario, specifically 3 and 15.
Here, a = 3 and b = 15.
We will follow the below steps to find out the exclusive OR of the given two operands.
- We know that 3 and 15 are in decimal form, and for applying bitwise XOR operation, it is necessary to convert it into binary form.
- Binary form ' a ', i.e., 3 is '11' and Binary form of ' b ', i.e., 15 is '1111'.
- Here we will observe that the count of binary digits present in a is two and the count of binary digits present in b is four; hence the number of binary digits present in both the variables are not the same. Thus, unbalanced, we do need to add more number of 0's on the left side of the lower binary number, i.e., a, which is ' 11' , to balance it.
- After balancing, the value of a is ' 0011 ' , and b is ' 1111 '.
- Now, putting the binary digits present in ' b ' down to the binary digits present in ' a '.
- Finally, applying the XOR operation one by one on the corresponding bits matches and note down the output.
- The output generated at last will be in binary form, as the above question given in the decimal form, so we need to convert the result in decimal form.
Explanation:
a = 3 ( In Decimal form )
b = 15 ( In Decimal form )
Now, in order to perform the XOR operation between a and b, it is necessary to convert both a and b into their binary representations -
a = 0011 ( In Binary form )
b = 1111 ( In Binary form )
Now, applying XOR operation on a and b -
a = 0011
The variable b is assigned a value of 1111.
Performing a bitwise operation on a and b results in 1100 in binary representation.
The outcome of a raised to the power of b is 1100, represented in binary format.
Now translating the outcome into decimal representation, which equals 12.
3 ^ 15 = 12
NOTE: By using the above XOR truth table, the output of corresponding bits are generated.
We are about to perform the bitwise XOR operation on the numbers 3 and 15 within the context of C++ programming language, yielding the outcome of 12.
C++ code for above example:
//************************ C++ Code ******************************
#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
}
Output