0️⃣

Bitwise XOR

 
 
The bitwise XOR (exclusive OR) operation is a binary operation performed on individual bits of two operands. It is denoted by the symbol "^". The bitwise XOR operation compares the corresponding bits of the operands and produces a result bit based on the combination of the bits. It returns a 1 if the bits are different (one 0 and one 1), and a 0 if the bits are the same (both 0 or both 1).
Here's an explanation of how the bitwise XOR operation works:
  • The operation compares the corresponding bits of the operands and produces a result bit based on the following rules:
    • If the bits are different (one 0 and one 1), the result bit is set to 1.
    • If the bits are the same (both 0 or both 1), the result bit is set to 0.
Let's consider an example to illustrate the bitwise XOR operation:
 
Operand 1: 10101010 Operand 2: 11001100 Result: 01100110 Explanation: - Comparing the corresponding bits: - 1 XOR 1 = 0 - 0 XOR 1 = 1 - 1 XOR 0 = 1 - 0 XOR 0 = 0 Therefore, the bitwise XOR operation produces the result 01100110.
 
In programming languages, the bitwise XOR operation is used to manipulate individual bits within variables or perform bitwise operations on integers. It is often used in scenarios such as toggling specific bits, checking for differences between bit patterns, or implementing bit-level manipulations.
It's important to note that the operands for the bitwise XOR operation are typically integers or integer-like types. The operation is performed on each pair of corresponding bits from the binary representations of the operands.
 
 
In C++, the bitwise XOR operator (^) is used to perform a bitwise XOR operation on the corresponding bits of two operands.
 
#include <iostream> using namespace std; int main() { int a = 10; // Binary representation: 1010 int b = 6; // Binary representation: 0110 // Bitwise XOR operation using the ^ operator int c = a ^ b; // Binary representation: 1100 cout << "a ^ b = " << c << endl; // Output: a ^ b = 12 a = 20; // Binary representation: 10100 b = 14; // Binary representation: 01110 c = a ^ b; // Binary representation: 11010 cout << "a ^ b = " << c << endl; // Output: a ^ b = 26 return 0; }
 
In this code, the integer variables a and b are initialized with decimal values 10 and 6 respectively. The bitwise XOR operator ^ is used to perform the bitwise XOR operation on a and b, and the resulting value is stored in the integer variable c. The program then outputs the result of the operation using cout.
The binary representation of a and b are also shown in the comments of the code. To perform the bitwise XOR operation, the corresponding bits of a and b are compared using the ^ operator to determine the resulting bit. The resulting integer value is the concatenation of the resulting bits.
The second section of the code demonstrates the use of the bitwise XOR operator on different values of a and b. The resulting integer value is the concatenation of the resulting bits after performing the bitwise XOR operation.
Bitwise XOR operation is useful for a variety of applications such as flipping specific bits in a number, testing two values for equality (by comparing the bitwise XOR of both values to 0), and encryption.
 
 
PRACTICE PROBLEMS :
  1. XOR Queries of a Subarray
  1. Chalkboard XOR Game