std::bit_xor in C++ with Examples

The bit_xor is an inbuilt function in C++ which is used to perform bitwise_xor and return the result after applying the bitwise_xor operation on it’s arguments.

Header File:

#include <functional.h>

Template Class:

template <class T> struct bit_xor;

Parameters: It accepts a parameter T which is the type of the argument to be compared by the functional call.

Note:



  1. Objects of this class can be used on standard algorithms such as transform or accumulate.
  2. Member functions ( ‘operator()’ ) returns the bit_xor of its arguments.

We must include the library ‘functional’ and ‘algorithm’ to use bit_xor and transform() respectively.

Below is the illustration of bit_xor in C++:
Problem 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate bit_xor in C++
  
#include <algorithm>
#include <functional> // to include bit_xor
#include <iostream>
#include <iterator>
using namespace std;
  
int main()
{
    // declaring the values
    int A[] = { 1, 2, 3, 4, 5, 6 };
    int B[] = { 6, 7, 8, 4, 5, 0 };
    int n = 6;
    // defining result
    int result[n];
  
    // transform is used to apply bitwise_xor
    // on the arguments A and B
    transform(A, end(A), B,
              result, bit_xor<int>());
  
    // printing the resulting array
    cout << "A xor B = ";
    for (const int& answer : result)
        cout << ' ' << answer;
  
    return 0;
}

chevron_right


Output:

A xor B =  7 5 11 0 0 6

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate bit_xor in C++
  
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
using namespace std;
  
int main()
{
    // declaring the values
    int A[] = { 0, 0xff, 15, 22 };
    int B[] = { 1, 255, 0xfa, 0x16 };
    int n = 4;
    // defining result
    int result[n];
  
    // transform is used to apply bitwise_xor
    // on the arguments A and B
    transform(A, end(A), B,
              result, bit_xor<int>());
  
    // printing the resulting array
    cout << "A xor B = ";
    for (const int& answer : result)
        cout << ' ' << answer;
  
    return 0;
}

chevron_right


Output:

A xor B =  1 0 245 0

Reference: https://en.cppreference.com/w/cpp/utility/functional/bit_xor

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.