Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do a lot of languages lack a logical XOR operator?

Off the top of my head, I cannot think of a single language I've used that had a logical exclusive or operator, but all have logical and bitwise and and or operators.

Looking around, the only reason to this that I could find was that exclusive or cannot be short circuited, so a logical version would be useless, which I really can't see being the case. The reason it came to my attention that most languages lack this is that I needed it (I was using Ruby, so I wrote a method to convert an integer to a boolean, and then use bitwise XOR, which on booleans acts like logical XOR).

Just using bitwise XOR does not work either, because it will give a different result.

0b0001  ^  0b1000 = 0b1001 (True)
0b0001 XOR 0b1000 = False
// Where ^ is bitwise exclusive or and XOR is logical exclusive or
// Using != (not equal to) also doesn't work
0b0001 != 0b1000 = True

So why is it that most languages do not include a logical exclusive or operator?

Edit: I added an example with how != also does not do what I want, it almost does, but falls into the same problem that using bitwise exclusive or does, it only works if you know that you are working with zero or one, and not any other number.

And to note, this is assuming that language uses zero as false and nonzero as true.

like image 408
Jeffrey Aylesworth Avatar asked Dec 09 '09 03:12

Jeffrey Aylesworth


People also ask

What is the result of the logical XOR operation?

The result of logical XOR operation is equal to 1 (one) if one of the bits of a or b equal to 1 (one), and in all other cases, the result is 0 (zero). Look at the truth table of the XOR logical operation.

What is the result of performing a logical OR operation?

The result of performing a logical OR operation will be 0 if a and b are equal to 0 (zero), and in all other (other) cases, the result is equal to 1 (one). Look at the truth table of the OR logical operation. XOR logical operation is performed with two bits (a and b).

What are the basic logical operations and examples?

The following are the basic logical operations and examples of their use. AND logical operation is performed with two bits; let’s call them a and b. The result of executing the logical AND operation is equal to 1 if a and b are equal to 1; in all other cases, the result will be 0. Look at the truth table of the AND logical operation:

What are logical or Boolean operations?

These operations are called logical or Boolean operations, and were named after the mathematician George Boole (1815-1864), who contributed to the development of this field of science. All these operations can be applied to any bit, regardless of whether its state is 0 (zero) or 1 (one).


2 Answers

What do you mean by "logical XOR operator"? I'm not sure what result do you expect from your examples, but here's my answer:

a (logical XOR) b is the same as bool(a) != bool(b)

Inequality is a logical XOR. Since you already have the bitwise XOR version, you don't need a special operator for the logical one.

like image 75
viraptor Avatar answered Dec 01 '22 23:12

viraptor


Nearly every language has a logical XOR. The symbol they use for it varies, but regardless of the symbol, most people pronounce it "not equal".

Edit: for those who doubt, test code for three variables:

#include <iostream>

int main() { 
    for (int i=0; i<2; i++)
        for (int j=0; j<2; j++)
            for (int k=0; k<2; k++) {
                std::cout << "\n!=" << (i!=j!=k);
                std::cout << "\t^ " << (i^j^k);
            }
    return 0;
}
like image 23
Jerry Coffin Avatar answered Dec 01 '22 21:12

Jerry Coffin