Use of C++ XOR operator

Use of C++ XOR operator
Techiio-author
Written by Sagar RabidasJanuary 28, 2022
8 min read
C++
1 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss the C++ XOR operator.

C++ XOR operator

The xor operator is one of the bitwise operators in c++, which takes two operators as the operands and on every little bit of the two operands. The xor operation is performed, and the result of the xor operation at the given two bits of the 2 operands is 0. If the 2 bits of the given two operands is 0 and the result of xor operation on the given two bits of the two operands is 0 if the 2 bits of the given two operands is one and the result of xor operation on the given two bits of the 2 operands is one if one of the two bits of the given two operands is 0 or one and the alternative bit is one or 0, this is if the 2 bits of the given two operands are exclusive, the result is one.

Syntax of XOR operator in C++

variable1 ^ variable2

Working of XOR operator in C++

  • The XOR operator is one of the bitwise operators in C++, which takes two operators as the operands, and on each bit of the two operands, the XOR operation is performed.
  • The result of the XOR operation on the given two bits of the two operands is zero if the two bits of the given two operands are zero.
  • The result of the XOR operation on the given two bits of the two operands is zero if the two bits of the given two operands are one.
  • The result of XOR operation on the given two bits of the two operands is one if one of the two bits of the given two operands is zero or one and the other bit is one or zero, that is, if the two bits of the given two operands are different, the result is one.

Examples of C++ XOR operator

Different examples are mentioned below:

Example #1

C++ program to demonstrate the XOR operator in C++ to perform XOR operation on the given two operands and display the result:

Code:

//The header iostream is included to be able to make use of cin and cout statements
#include <iostream>
using namespace std;
//main method is called
int main()
{
//an integer variable called a is defined to store the first integer value
int a = 5;
//an integer variable called b is defined to store the second integer value
int b = 4;
//an integer variable called c is defined to store the result of XOR operation on the two values stored in the variables a and b
int c = a ^ b;
cout << "The result after performing XOR operation on a and b is " << c;
return 0;
}

Example #2

C++ program to demonstrate the XOR operator in C++ to perform XOR operation on the given two operands and display the result:

Code:

//The header iostream is included to be able to make use of cin and cout statements
#include <iostream>
using namespace std;
//main method is called
int main()
{
//an integer variable called a is defined to store the first integer value
int a = 20;
//an integer variable called b is defined to store the second integer value
int b = 35;
//an integer variable called c is defined to store the result of XOR operation on the two values stored in the variables a and b
int c = a ^ b;
cout << "The result after performing XOR operation on a and b is " << c;
return 0;
}

Example #3

C++ program to demonstrate the XOR operator in C++ to perform XOR operation on the given two operands and display the result:

Code:

//The header iostream is included to be able to make use of cin and cout statements
#include <iostream>
using namespace std;
//main method is called
int main()
{
//an integer variable called a is defined to store the first integer value
int a = 100;
//an integer variable called b is defined to store the second integer value
int b = 35;
//an integer variable called c is defined to store the result of XOR operation on the two values stored in the variables a and b
int c = a ^ b;
cout << "The result after performing XOR operation on a and b is " << c;
return 0;
}

C
C++
C++ XOR operator
1 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance