Xor Eq In C++

In this article, you will learn about the Xor_eq in C++ with their examples.

The eleven keywords in C++ are simply different ways of writing words that are normally denoted by alphanumeric characters . These keywords are also well-handled in the preprocessor. They cannot be reported as variables or guidelines; Instead, they act as operators in the #if. You can request that those keywords be translated to C++ by adding iso646.h in C++ to C. Each header is defined as an object-like macro that extends to the appropriate punctuation marks.

The following are the designated operators along with the respective punctuators:

  • and &&
  • and_eq &=
  • bitand &
  • bitor |
  • compl ~
  • not!
  • not_eq !=
  • or ||
  • or_eq |=
  • xor ^
  • xor_eq ^=
  • XOR(^):

  • A bitwise XOR operation is performed between the two operands using the XOR operation (^).
  • In the case where two bits do not match, the result is 1. If the two bits are equal, the result is 0.
  • For example, 1010 ^ 1100 gives 0110 (10 XOR 12 equals 6).
  • Assignment operators for XOR (^=):

  • By combining the left and right operands bittwise, the XOR function operator (^=) assigns the result to the left operand.
  • In this case, lhs is the left operand and rhs is the right operand. It is equivalent to lhs = lhs ^ rhs.
  • For example, a ^= b; and a = a^b; They are perfect.
  • Program 1:

Let us take an example to illustrate the Xor_eq function in C++.

Example

#include <iostream>
using namespace std;
int main() {
    int a = 10;
    int b = 7;
    a ^= b;  // Equivalent to a = a ^ b;
    cout << "a after XOR: " << a << endl;
    return 0;
}

Output:

Program 2:

Let us take another example to illustrate the Xor_eq function in C++.

Example

#include <iostream>
#include <string>
using namespace std;
string xor_encrypt_decrypt(const string& input, const string& key) {
    string output;
    for (size_t i = 0; i < input.length(); ++i) {
        output += input[i] ^ key[i % key.length()];
    }
    return output;
}
int main() {
    string plaintext, key;
    cout << "Enter plaintext: ";
    getline(cin, plaintext);
    cout << "Enter key: ";
    getline(cin, key);
    // Encryption
    string ciphertext = xor_encrypt_decrypt(plaintext, key);
    cout << "Encrypted: " << ciphertext << endl;
    // Decryption
    string decrypted = xor_encrypt_decrypt(ciphertext, key);
    cout << "Decrypted: " << decrypted << endl;
    return 0;
}

Output:

Input Required

This code uses input(). Please provide values below: