C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

C++ 'xor' Keyword with Examples

C++ | 'xor' keyword: Here, we are going to learn about the 'xor' keyword which is an alternative to EXCLUSIVE-OR operator.
Submitted by IncludeHelp, on May 16, 2020

C++ 'xor' Keyword

"xor" is an inbuilt keyword that has been around since at least C++98. It is an alternative to ^ (EXCLUSIVE-OR) operator and it mostly uses for bit manipulations.

The xor keyword compares two bits and returns 1 if the bits are complementary; otherwise, it returns 0.


operand_1 xor operand_2;

Here, operand_1 and operand_2 are the operands.

Sample Input and Output

bitset<4> value("1100");
bitset<4> mask ("1010");
value = value xor mask;

value = 0110

C++ example to demonstrate the use of "xor" keyword

// C++ example to demonstrate the use of
// 'xor' keyword

#include <iostream>
#include <bitset>
using namespace std;

int main()
    bitset<4> value("1011");
    bitset<4> mask1("1100");
    bitset<4> mask2("0100");

    // before operation
    cout << "value: " << value << endl;
    cout << "mask1: " << mask1 << endl;
    cout << "mask2: " << mask2 << endl;

    value = value xor mask1;

    cout << "After operation (1)...\n";
    cout << "value: " << value << endl;

    value = value xor mask2;

    cout << "After operation (2)...\n";
    cout << "value: " << value << endl;

    return 0;


value: 1011
mask1: 1100
mask2: 0100
After operation (1)...
value: 0111
After operation (2)...
value: 0011

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.