• Home
  • 3.16 Bitwise XOR Operator (^)

3.16 Bitwise XOR Operator (^)

View Categories

3.16 Bitwise XOR Operator (^)

1 min read

Table of Contents

The bitwise XOR (exclusive OR) operator (^) performs a logical exclusive OR operation on each corresponding bit of two integral operands. A result bit is set to 1 when the corresponding bits are different. If both bits are the same, the result bit is 0.

Bitwise XOR is commonly used for toggling bits, simple encryption techniques, parity calculations, and detecting differences between binary values.

Source Code #

#include <iostream>

int main()
{
    int num1 = 12;
    int num2 = 10;

    int result = num1 ^ num2;

    std::cout << "First Number : " << num1 << '\n';
    std::cout << "Second Number: " << num2 << '\n';
    std::cout << "Result       : " << result << '\n';

    return 0;
}

Output #

First Number : 12
Second Number: 10
Result       : 6

Explanation #

  • The bitwise XOR operator (^) compares the corresponding bits of two integral operands.
  • A result bit is set to 1 when the corresponding operand bits are different.
  • A result bit is set to 0 when both corresponding operand bits are the same.
  • The operands 12 and 10 have the following binary representations:12 = 1100â‚‚ 10 = 1010â‚‚ -------------- ^ = 0110â‚‚
  • The binary result 0110â‚‚ is equal to the decimal value 6.
  • Bitwise XOR operations are commonly used to toggle bits, compare binary values, and perform parity calculations.
  • The bitwise XOR operator is applicable only to integral data types.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *