In this guide, you will explore the Xor_eq operation in C++ along with illustrative examples.
The eleven reserved words in C++ are essentially alternative representations of terms typically represented by alphanumeric characters. These reserved words are also effectively managed during the preprocessing stage. They are not classified as identifiers or directives; rather, they function as operators in conditional directives like #if. To convert these reserved words to C++, you can include the iso646.h header in your C++ program. Each keyword in the header is defined as a macro that expands to the corresponding punctuation symbols.
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 ^=
- 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).
- 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.
XOR(^):
Assignment operators for XOR (^=):
Program 1:
Let's consider a scenario to demonstrate the Xor_eq function in C++.
#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's consider another instance to demonstrate the Xor_eq function within C++.
#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: