×

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.

XYZ =  X ^ Y
000
011
101
110

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.

XYZ = X ^ Y
FFF
FTT
TFT
TTF

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.


Related Topics

How to enter a name in C++

A name is a string or array of characters or letters. The string is one of the most helpful data types offered by the C++ library. A string helps the...

4 minutes read.

Maps in C++

Maps: Maps in C++ are the containers associated with key and mapped values. By keys and mapped values, we mean that the maps are used to store elements formed by the...

4 minutes read.

C++ Tricks for Competitive Programming

If you are interested in Computer science or Information technology, you must have heard about competitive programming. Competitive programming is a way to improve your problem-solving skills. There are various...

7 minutes read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

External merge sort in C++

External merge sort in C++ External sorting is a concept for a group of sorting algorithms capable of handling large data volumes. External sorting is needed if the information getting sorted...

9 minutes read.

Pattern programs in C++

In this article, we are going to discuss different pattern programs in C++. Program for Printing * patterns: Right Angle Triangle* pattern #include <iostream> using namespace std; int main() {     int rows;     cout <<...

3 minutes read.

Diamond Pattern in C++ using For Loop

For Loop: A for loop is a repetitive control structure that allows you to create a loop for executing a specific number of times. The syntax of for loop: In C++, a for...

5 minutes read.

Loops in C++

A loop statement in most programming languages allows us to execute a statement or a collection of statements numerous times. Control structures of programming languages vary, allowing for more complex...

6 minutes read.

C++ find missing in the second array

Given two arrays A and B of sizes n and m. Find the elements from array A that are not present in array B. Example Input : a[] = {1, 2, 3, 5,...

3 minutes read.

C++ Bidirectional Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

Constexpr in C++

Constexpr is a keyword in C++ that is used to declare variables or functions as compile-time constants. This means that the value of a constexpr variable or the return value...

3 minutes read.

C++ | C Plus Plus Data type

Data type in every language is very important. Data type means the different kinds of data that are supported by a particular programming language. A computer language’s data types specify the...

15 minutes read.

Singleton Design Pattern in C++

Singleton is similar to the global variable. Singleton helps to have only one object of its kind and provides only single access. One of the key features of the singleton...

2 minutes read.

C++ Namespaces

An Overview In each scope, a name can only represent one entity. As a result, there cannot be two independent variables with the similar names in the same scope, as this may cause...

10 minutes read.

Data Hiding in C++

C++ : High-performance apps can be made using the cross-platform language C++. Bjarne Stroustrup created C++ as an addition to the C language. Programmers have extensive control over memory and system...

3 minutes read.

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on...

4 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

6 minutes read.