C++

C++ XOR

XOR is a type of bitwise operator. The bitwise operators are the comparison operators that compare the bytes of the operand values in the binary notation. The XOR operator is the one that contains the two operands. This operation is applied to the integer value. There are almost six bitwise operators in C++.

AND operator (&), OR operator (|), XOR operator (^), Complement operator (~), shift left operator (<<), and shift right operator (>>).

Syntax of XOR

# VARIABLE1 ^ VARIABLE2

Variable 1 is the first operand on which the operation is to be performed. And variable 2 is the second operand. ‘^’ is the symbol of the XOR operator. This symbol is used between the two variables on which the XOR operation is to be applied.

C++ XOR Working

The XOR operator is the type of bitwise operator that has two operands and on each bit of these two operands, the operation of XOR is performed.

If somehow the two bits of such two operands are both zero, the resultant value returned by the XOR is zero.

The resultant is also 0 if both the values of the numbers in operands are 0.

Similarly, the result of XOR is one if and only if the two bits of two operands are 1.

Whereas the returned value is only 1 when both the bits of two operands are different. Whether the first operand is 1 and the second is zero or vice-versa.

The value of two operand values is converted into the binary value in the form of (0-1) bit. After that, we apply the XOR operation on both binary values. This binary notation is now converted into decimal form. This is the integer value similar to those values of the operands.

The Truth Table for the XOR Operator

Operand a Operand b A ^ b
0 0 0
0 1 1
1 0 1
1 1 0

Implementation of C++ XOR

Example 1

A simple example to explain the working of XOR in C++ is explained here. We need to have two variables that will contain two values on which we will apply the XOR function. First, the library is used in the header file to enable the use of cin and cout streams in the program that is known to be the iostream.

#include <iostream>

Now in the main program, the two variables will be initialized with the values. First, these two values will be converted into the binary notation of these numbers.

Then on each binary, the XOR operation will be applied. As we know that 1 is returned if anyone operand is 1, otherwise 0 is returned. By following these rules, the resultant of this value will be obtained. And then the binary answer will be converted into decimal notation.

The binary value of 12 is 00001100

The binary value of 25 is 00011001

Apply XOR operation on both values.

00001100
^ 00011001
_________
00010101

This is the binary value of 21 in the decimal notation.

Save the file with the extension of c and then, execute it. To execute a C++ program, we need to have a compiler of a C++ programming language. So for this purpose, a G++ compiler is used. This compiler uses an input file and displays the results. ‘-o’ is used to save the resultant in the output file.

$ g++ -o xor xor.c
$ ./xor

Example 2

In this example, we will find an uneven or a different value amongst the list of numbers that does not match with the other elements in the list. It is also an application of XOR that it finds the number that is different from others. A couple of numbers are provided and an odd occurring number is identified.

After using the iostream library, we have created a separate function outside the main body. This function will return the odd element in the array. This function will contain the array and the integer variable as a parameter because the variable will contain the size of the array initialized in the main program. A variable is also introduced here that will store the odd number calculated by XOR. As we know that to access each index of the array, we need to use a loop that iterates and increments in each step.

The loop will iterate till the size of an array, and in each iteration, the variable will calculate the odd number and store it.

Res ^= arr[i];
The value will be returned to the main program. In the main program, an array with some numbers is initialized. The size of the array is obtained and then a function call is made in the cout statement to display the odd number among all the same numbers.

Findodd( arr, n)
Save the above code and compile it in the terminal. You will see that the number 90 is the one that is uncommon in the whole array.

Example 3

This example is about the combined use of some bitwise operators. So after using the library, we will head towards the main program. Just like XOR, AND, and the OR operator works on two values minimum. Firstly, we will initialize two variables with the numbers. In each bitwise operation, every number will be first converted into the binary value to apply the operators, and then the results will be converted into decimal numbers, as we did in the previous example. Now, we will apply AND operator. According to this operator, both the operands must have 1 value so that the AND operator becomes true and returns 1, in the case of any 0, it will return false.

A & b;
A binary resultant value will be obtained and then the decimal conversion will be followed. The next one is the OR operator. For this bitwise operation, only a single operand must be 1, so that it can return 1, and if both operands are 1, then again 1 will be returned.

A | b;
Again, convert the binary into the decimal value. And the last operator is the XOR operator, for which we know that it returns 1 if and only if any one of its operands is 1. Otherwise, it is 0.

A ^ b;
Now, save and execute the file. You will see that each operator has worked effectively to calculate the resultant values.

Important Facts About Bitwise Operators

The bitwise operators must not be used in the place of the logical operators. Because the result of the logical operator is 1 or 0. (logical operators are AND, OR, NOT). The value that is obtained is an integer. This is because the decimal value is converted into binary value and then again, the binary is converted into a decimal value that is an integer value.

Conclusion

Now, we will summarize the ‘C++ XOR’ topic. We have explained the bitwise XOR operators by defining all types. The working of XOR is also explained in the C++ programming language. Its work includes the truth table involvement that is also mentioned above. Some examples are included here to demonstrate the knowledge of the XOR bitwise operator.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.