×

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

The Stock Span Problem

It is necessary to determine the span of a stock's price throughout all n days in order to solve the stock span problem, which involves a set of n daily...

5 minutes read.

C++ Interfaces

The C++ programming language provides programmers with a variety of capabilities and functions. It also enables object-oriented programming, which is essential while working on a project. It will be simple...

7 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++ Global Variable

In C++, a global variable is a variable that is defined outside of any function or class and can be accessed by any function or class in the program. Global...

4 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

Computing index using Pointers Returned by STL Functions in C++

In this tutorial we will learn how to compute index using pointers which returned by STL functions in C++. Many built-in C++ functions return pointers to memory places that provide...

2 minutes read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 minutes read.

How to Reverse a String in C++ using While Loop

A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer...

6 minutes read.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

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

Differences between #define & const in C/C++

 Differences between #define & const in C/C++ A preprocessor directive is #define. The preprocessor replaces things defined by #define prior to starting compilation. In this chapter, we'll learn about the member, variable,...

3 minutes read.

Delete Operator in C++

Overview We can reserve allocation for a variable or an array at runtime in C++. Dynamic memory allocation is the term for this. After utilising a variable in C++, we must...

4 minutes read.

C++ Overriding

C++ Function Overriding When the base and derive class both contain the same function name and calling the function through derived object invokes derived class function called function overriding. Function overloading is...

1 minute read.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

How to Reverse a String in C++ using Do-While Loop

Strings In C++, a string is an object that represents a group (or sequence) of various characters. Strings are part of the standard string class in C++ (std::string). The characters of...

4 minutes read.

Preventing Object Copy in C++

C++ is an object-oriented programming language that provides the ability to create objects, define class and pass objects to functions. When passing an object to a function or returning an...

7 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.

Iostream in C++

Using Iostream in C++, we can perform input and output operation capabilities. This represents input and output, and the stream is used to carry out this capability. A stream is...

4 minutes read.

Factory Method for Designing Pattern in C++

In C++, the factory method is a type of conditional design pattern. The factory method is related to creating a new object in C++. With the help of a factory...

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