Javascript has a xor operator?

Asked

Viewed 2,743 times

8

There is an xor operator in javascript and what would be the 'symbolic' form of it (for example or has || and has &&)?

4 answers

7


Javascript does not have a logical XOR operator.

It has a bit-by-bit XOR operator that can perform a bit-by-bit comparison of two numbers, but this does not help when you want to get the result of a two-expression XOR, which does not return a number.

What you can do is create a function to do this kind of logical operation:

function myXOR(x, y) {
  return (x || y) && !(x && y);
}


if (myXOR(hasValue(value1), hasValue(value2))) {
  //FAZ ALGUMA COISA
}

Here are some references to better understand how logical operations work in JS.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#operadores_bit_a_bit

http://www.howtocreate.co.uk/xor.html

https://docs.microsoft.com/en-us/scripting/javascript/reference/bitwise-xor-operator-decrement-hat-javascript

4

You can use the operator ^ as described in that reply

Ex.:

var nb = 5^9 // = 12

For boolean values you can convert with !!, that will reverse the result turning it into boolean, then reversing it again

Ex.:

!!(false ^ true)

4

If you analyze the XOR truth table, also known as "Or exclusive", you will see that the output is true only when the inputs are different.

┌───────────┬───────────┬───────┐
│ entrada_a │ entrada_b │ saida │
├───────────┼───────────┼───────┤
│ false     │ false     │ false │
│ false     │ true      │ true  │
│ true      │ false     │ true  │
│ true      │ true      │ false │
└───────────┴───────────┴───────┘

That said, it’s quite simple to implement a xor corresponding to the table above, because the result is true only if the entries are different. Just returns entrada_a != entrada_b.

function xor(a, b) {
    return a !== b;
}

I’m using strict comparison, but you should use depending on your need, understand the difference in "What is the difference between the operators == and == in Javascript?".


Remembering that the XOR operator has commutativity, that is to say:

(a ^ b) ^ c == a ^ (b ^ c)

Using operator != as a xor maintains the commutativity, see examples:

(0 ^ 1) ^ 1; // 1
0 ^ (1 ^ 1); // 1

(0 !== 1) !== 1; // true
0 !== (1 !== 1); // true
  • Well, not exactly... freak out xor(null, undefined) or (4, 2) he returns true, what is wrong. A better implementation would be function xor(a, b) { return !!a !== !!b }. Boolean value has to be different, not real value.

  • But then comes validation of inputs that is not the scope of the answer (the truth table shows that valid inputs are only true and false). Are you using as an example that null and undefined would be valid values for the function. These specific peculiarities of your case should be adjusted according to need. For demonstration of concept, I believe that adding this information would be unnecessary in the answer.

3

Look, in order not to have to convert to boolean, you can "improvise" a XOR using AND and if/Else. Here follows a code snippet, using two variables p and q.

var p = false
var q = true

p = window.confirm("Selecione 'OK' para p==true, e 'Cancelar' para p==false.")
q = window.confirm("Selecione 'OK' para q==true, e 'Cancelar' para q==false.")

if (!p && q){
  window.alert ("A expressão é verdadeira.")
}else{
  if (!q && p){
    window.alert ("A expressão é verdadeira.")
  }else{
    window.alert ("A expressão é falsa.")
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.