How to do XOR in C++?
In C++ or any other programming language, there are some basic things or expressions that we use to make more complex things, such as, when we talk about data structures and algorithms, we are simply using simple data structures, such as a simple array, and combining them with other algorithms to make complicated things. One such thing is XOR, which is called exclusive OR. It's a simple expression yet very important. In C++, XOR is very important as we can use it to make many cool stuff and work with it to make our code very hard to crack while working with very large datasets. In this tutorial, we will understand about XOR and see its implementation.
Understanding XOR
XOR is basically like a math tool that generally takes two parts of information or data while computing something. If both the parts of the data are the same or equal, such as if both are either 0 or 1, then it will return 0 in that case, and if the parts are different, such as one is 0 and one is 1, then it will return 1. In C++, when we want to implement it, we usually use the "^" symbol to implement it in our code. One of the coolest features of XOR is that it doesn't matter in which order we are putting the elements. It will be grouped and evaluated on its own.
Implementation in C++
In C++, implementing XOR is straightforward, thanks to the "^" operator. Here's a basic example demonstrating XOR usage:
Example:
#include <iostream>
#include <bitset>
#include <string>
#include <vector>
// Function to perform XOR encryption and decryption
std::string xor_encrypt_decrypt(const std::string& message, const std::string& key) {
std::string result;
for (size_t i = 0; i < message.length(); ++i) {
result += message[i] ^ key[i % key.length()];
}
return result;
}
// Function to perform XOR operation on two vectors of integers
std::vector<int> xor_vectors(const std::vector<int>& vec1, const std::vector<int>& vec2) {
std::vector<int> result;
size_t min_size = std::min(vec1.size(), vec2.size());
for (size_t i = 0; i < min_size; ++i) {
result.push_back(vec1[i] ^ vec2[i]);
}
return result;
}
// Function to calculate XOR checksum of a binary string
int calculate_xor_checksum(const std::string& binary_string) {
int checksum = 0;
for (char bit : binary_string) {
checksum ^= (bit - '0'); // converting char to int
}
return checksum;
}
// Function to toggle a specific bit in an integer
int toggle_bit(int num, int pos) {
return num ^ (1 << pos);
}
int main() {
// Example 1: XOR encryption and decryption
std::string plaintext = "Hello, world!";
std::string key = "secret";
std::string encrypted = xor_encrypt_decrypt(plaintext, key);
std::cout << "Encrypted message: " << encrypted << std::endl;
std::string decrypted = xor_encrypt_decrypt(encrypted, key);
std::cout << "Decrypted message: " << decrypted << std::endl;
// Example 2: XOR operation on vectors
std::vector<int> vec1 = {1, 0, 1, 0, 1};
std::vector<int> vec2 = {0, 1, 0, 1, 0};
std::vector<int> result_vector = xor_vectors(vec1, vec2);
std::cout << "Result of XOR operation on vectors:";
for (int num : result_vector) {
std::cout << " " << num;
}
std::cout << std::endl;
// Example 3: XOR checksum calculation
std::string binary_string = "101010101"; // Example binary string
int checksum = calculate_xor_checksum(binary_string);
std::cout << "XOR checksum of binary string '" << binary_string << "': " << checksum << std::endl;
// Example 4: Toggling a specific bit
int num = 10; // binary representation: 1010
int pos = 2; // toggle 3rd bit (0-based indexing)
int toggled_num = toggle_bit(num, pos);
std::cout << "Original number: " << num << ", Toggled bit at position " << pos << ": " << toggled_num << std::endl;
return 0;
}
Output:
![How to do XOR in C++?/>
<!-- /wp:html -->
<!-- wp:paragraph -->
<p><strong>Example 2:</strong></p>
<!-- /wp:paragraph -->
<!-- wp:preformatted -->
<pre class=](https://static.tutorialandexample.com/cpp/how-to-do-xor-in-cpp1.png)
// Function to perform XOR operation
int performXOR(int t, int m) {
return t ^ m;
}
int main() {
int val1, val2;
// Input the numbers from the user
std::cout << "Enter the first number: ";
std::cin >> val1;
std::cout << "Enter the second number: ";
std::cin >> val2;
// Perform an XOR operation
int result = performXOR(val1, val2);
// Display the result
std::cout << "Result of XOR operation: " << result << std::endl;
return 0;
}
Output:
![How to do XOR in C++?/>
<!-- /wp:html -->
<!-- wp:heading -->
<h2 class=](https://static.tutorialandexample.com/cpp/how-to-do-xor-in-cpp2.png)
XOR is a really important tool in the field of computer science. It's a simple tool, but learning it can help us with a lot of tasks and work. For example, we can use it to make our code complex or to create something complex, to keep our data safe and to move data around. It can be really beneficial when you learn it. Whether we are trying to hide information or move data around, XOR can be a really powerful tool.