Logical XOR in C++

Suraj P Oct 12, 2023
  1. Logical XOR in C++
  2. Use the != Operator to Implement Logical XOR With Boolean Operands in C++
  3. Use the Modulus 2 Operation to Implement Logical XOR With Boolean Operands in C++
  4. Use the ^ Operator to Implement Logical XOR With Boolean Operands in C++
  5. Use the if-else Statement to Implement Logical XOR With Boolean Operands in C++
  6. Use the Ternary Operator to Implement Logical XOR With Boolean Operands in C++
  7. Conclusion
Logical XOR in C++

Logical XOR (exclusive OR) is a fundamental operation in computer programming used to evaluate true/false conditions based on two Boolean operands. In C++, logical XOR can be implemented using several approaches, including the != operator, the ^ operator (bitwise XOR), if-else statements, and the ternary operator.

In this article, we will explore these methods and demonstrate how to implement logical XOR in C++.

Logical XOR in C++

In C++, although we have logical OR (||) and logical AND (&&) operators, there isn’t a built-in logical XOR operator. However, we can achieve the functionality of logical XOR using existing operators.

The truth table for XOR is as follows:

A | B | A XOR B
--|---|--------
0 | 0 |   0
0 | 1 |   1
1 | 0 |   1
1 | 1 |   0

As you can see, the result is true (1) only when the operands are different. By analyzing this, we can observe that XOR is akin to an inequality checker for Boolean values.

The logical XOR operator is essentially a NOT EQUAL TO (!=) operator when applied to Boolean values. Therefore, the general syntax for logical XOR is:

A logical XOR B is equivalent to A != B

The expression A != B will evaluate to true if A and B are not equal, effectively simulating the logical XOR operation.

Use the != Operator to Implement Logical XOR With Boolean Operands in C++

In C++, the logical XOR operation is commonly used to determine if two Boolean values are not equal. Let’s start by demonstrating how to use logical XOR with Boolean operands in C++:

#include <iostream>
using namespace std;

int main() {
  bool a = false;
  bool b = true;

  if (a != b) {
    cout << "Yes\n";
  } else
    cout << "No\n";
}

Output:

Yes

In this example, two Boolean variables, a and b, are declared and assigned Boolean values (false and true, respectively). The subsequent if-else control structure evaluates whether a is not equal to b, effectively simulating a logical XOR operation.

If the condition is true (indicating that a and b are different), Yes is outputted to the console. Conversely, if a is equal to b, signifying they are the same, No is displayed.

In this specific case, where a is false and b is true, the condition is met (a != b), resulting in Yes being printed to the console.

Use the != Operator to Implement Logical XOR to Non-Boolean Operands in C++

In cases where the operands are non-Boolean, we need to convert them to Boolean first and then apply the NOT EQUAL TO (!=) operator to mimic logical XOR.

Here’s how you can achieve this:

#include <iostream>
using namespace std;

int main() {
  int a = 10;
  int b = 20;

  if ((!a) != (!b)) {
    cout << "Yes\n";
  } else
    cout << "No\n";
}

Output:

No

Here, two integer variables, a and b, are declared and initialized with values (10 and 20, respectively). The if-else control structure evaluates the logical XOR of the negation of a and b (using ! to negate), determining whether their negations are not equal.

If the condition is true (indicating that the negations of a and b are different), Yes is printed to the console. Conversely, if the negations of a and b are equal, No is displayed.

In this specific case, !a evaluates to false (0), and !b evaluates to false (0) as well. Therefore, the condition ((!a) != (!b)) is false, and No is printed to the console.

Use the != Operator to Implement Logical XOR With Complex Expressions in C++

The concept of converting values to Boolean and applying the NOT EQUAL TO (!=) operator can further be extended to perform logical XOR operations for complex expressions.

Here’s an example:

#include <iostream>
using namespace std;

int main() {
  int a = 10;
  int b = 20;

  int x = 50;
  int y = 100;

  if (!(a < b) != !(x >= y)) {
    cout << "Yes\n";
  } else
    cout << "No";
}

Output:

Yes

In this example code, four integer variables (a, b, x, and y) are declared and assigned values. The if-else control structure evaluates the logical XOR of two expressions involving negations (!) and relational operators (<, >=).

Specifically, it checks whether the negation of a < b is not equal to the negation of x >= y. In this case, a < b evaluates to true (1) and x >= y evaluates to false (0).

Therefore, the condition (!(a < b) != !(x >= y)) is true, and Yes is printed to the console.

Use the Modulus 2 Operation to Implement Logical XOR With Boolean Operands in C++

An alternative method to implement logical XOR between two Boolean values is by utilizing the modulus 2 (% 2) operation. The logical XOR of A and B can be written as (A + B) % 2.

Here’s a demonstration:

#include <iostream>
using namespace std;

bool XOR(bool x, bool y) { return (x + y) % 2; }

int main() {
  cout << "XOR(0, 0): " << XOR(0, 0) << endl;
  cout << "XOR(1, 0): " << XOR(1, 0) << endl;
}

Output:

XOR(0, 0): 0
XOR(1, 0): 1

Here, we start by defining a function named XOR that takes two Boolean values (x and y) and returns the XOR of the inputs using arithmetic operations. Inside the main() function, it calls the XOR function with different Boolean values and prints the results.

The XOR function calculates the XOR of x and y by adding them and then taking the modulo 2. If the sum is odd, the result is 1, indicating a true XOR; otherwise, it’s 0.

In the main() function, it calls XOR with (0, 0) and (1, 0) as inputs. The XOR of 0 and 0 is 0, and the XOR of 1 and 0 is 1. It then prints these results to the console.

Use the ^ Operator to Implement Logical XOR With Boolean Operands in C++

The bitwise XOR (^) operator in C++ compares each bit of two operands and returns a 1 if the corresponding bits are different and 0 if they are the same. This operation is often used in bitwise manipulation of integers, but it can also be employed to achieve logical XOR for Boolean operands.

To implement logical XOR using the ^ operator, we need to recognize that logical XOR returns true (1) only if the operands have different Boolean values. This aligns with the behavior of the bitwise XOR operator.

Therefore, we can use the ^ operator to achieve the desired logical XOR functionality.

Here’s a simple function that demonstrates implementing logical XOR using the ^ operator:

#include <iostream>

bool logicalXOR(bool a, bool b) { return a ^ b; }

int main() {
  bool value1 = true;
  bool value2 = false;

  std::cout << "Logical XOR of " << value1 << " and " << value2
            << " is: " << logicalXOR(value1, value2) << std::endl;

  return 0;
}

Output:

Logical XOR of 1 and 0 is: 1

Here, the logicalXOR function is defined to take two Boolean parameters, a and b. It returns the result of applying the bitwise XOR (^) operator on a and b, representing the logical XOR of the Boolean values.

Then, in the main function, two Boolean variables, value1 and value2, are declared and initialized to true and false, respectively. The std::cout statement is used to print the logical XOR of value1 and value2 using the logicalXOR function.

As we can see, the result is displayed as Logical XOR of 1 and 0 is: 1 since true XOR false evaluates to true in terms of logical XOR.

Use the if-else Statement to Implement Logical XOR With Boolean Operands in C++

Using if-else statements, we can also create a function that calculates the logical XOR of two Boolean values based on the definition of XOR. We’ll utilize the fact that XOR returns true if and only if the operands have different Boolean values.

Take a look at the code example below:

#include <iostream>

bool logicalXOR(bool a, bool b) {
  if ((a && !b) || (!a && b))
    return true;
  else
    return false;
}

int main() {
  bool value1 = false;
  bool value2 = true;

  std::cout << "Logical XOR of " << value1 << " and " << value2
            << " is: " << logicalXOR(value1, value2) << std::endl;

  return 0;
}

Output:

Logical XOR of 0 and 1 is: 1

Similar to the previous example, the logicalXOR function is defined to take two Boolean parameters, a and b. However, this time, it uses if-else statements to determine the logical XOR of a and b based on their Boolean values.

If either a is true and b is false, or a is false and b is true, it returns true; otherwise, it returns false.

In the main function, two Boolean variables, value1 and value2, are declared and initialized to false and true, respectively. The std::cout statement is used to print the logical XOR of value1 and value2 using the logicalXOR function.

The result is displayed as Logical XOR of 0 and 1 is: 1 since false XOR true evaluates to true in terms of logical XOR.

Use the Ternary Operator to Implement Logical XOR With Boolean Operands in C++

The ternary operator (?:) is a shorthand conditional operator in C++ that allows us to evaluate a condition and return one of two values based on that condition.

We can leverage the ternary operator to implement logical XOR by evaluating whether the operands have different Boolean values and return true accordingly.

Here is an example:

#include <iostream>

bool logicalXOR(bool a, bool b) { return (a ? !b : b); }

int main() {
  bool value1 = true;
  bool value2 = true;

  std::cout << "Logical XOR of " << value1 << " and " << value2
            << " is: " << logicalXOR(value1, value2) << std::endl;

  return 0;
}

Output:

Logical XOR of 1 and 1 is: 0

We have the same logicalXOR function defined to take two Boolean parameters, a and b, but here, we use the ternary operator to evaluate whether a is true or not.

If a is true, it returns the negation of b; otherwise, it simply returns b. This effectively implements the logical XOR operation by returning true when a is true and b is false, and false otherwise.

In the main function, two Boolean variables, value1 and value2, are declared and initialized to true. The std::cout statement is used to print the logical XOR of value1 and value2 using the logicalXOR function.

The output is displayed as Logical XOR of 1 and 1 is: 0 since true XOR true evaluates to false in terms of logical XOR.

Conclusion

Implementing logical XOR in C++ is essential for various programming tasks. Whether you choose to use the != operator, the ^ operator, if-else statements, or the ternary operator, understanding these approaches will help you efficiently implement logical XOR and improve your problem-solving skills in C++.

Choose the method that best fits your programming style and requirements.

Author: Suraj P
Suraj P avatar Suraj P avatar

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

LinkedIn GitHub

Related Article - C++ Operator