Is there anyway to implement XOR in javascript

前端 未结 18 578
孤城傲影
孤城傲影 2020-12-24 10:53

I\'m trying to implement XOR in javascript in the following way:

   // XOR validation
   if ((isEmptyString(firstStr) && !isEmptyString(secondStr)) |         


        
相关标签:
18条回答
  • 2020-12-24 11:44

    Here is an XOR function that takes a variable number of arguments (including two). The arguments only need to be truthy or falsy, not true or false.

    function xor() {
        for (var i=arguments.length-1, trueCount=0; i>=0; --i)
            if (arguments[i])
                ++trueCount;
        return trueCount & 1;
    }
    

    On Chrome on my 2007 MacBook, it runs in 14 ns for three arguments. Oddly, this slightly different version takes 2935 ns for three arguments:

    function xorSlow() {
        for (var i=arguments.length-1, result=false; i>=0; --i)
            if (arguments[i])
                result ^= true;
        return result;
    }
    
    0 讨论(0)
  • 2020-12-24 11:46

    Quoting from this article:

    Unfortunately, JavaScript does not have a logical XOR operator.

    You can "emulate" the behaviour of the XOR operator with something like:

    if( !foo != !bar ) {
      ...
    }
    

    The linked article discusses a couple of alternative approaches.

    0 讨论(0)
  • 2020-12-24 11:47

    I pretend that you are looking for a logical XOR, as javascript already has a bitwise one (^) :)

    I usually use a simple ternary operator (one of the rare times I use one):

    if ((isEmptyString(firstStr) ? !isEmptyString(secondStr) 
                                 : isEmptyString(secondStr))) {
    alert(SOME_VALIDATION_MSG);
        return;
    }
    

    Edit:

    working on the @Jeff Meatball Yang solution

    if ((!isEmptyString(firstStr) ^ !isEmptyString(secondStr))) {
      alert(SOME_VALIDATION_MSG);
      return;
    }
    

    you negate the values in order to transform them in booleans and then apply the bitwise xor operator. Maybe it is not so maintainable as the first solution (or maybe I'm too accustomed to the first one)

    0 讨论(0)
  • 2020-12-24 11:47

    Try this: function xor(x,y) var result = x || y if (x === y) { result = false } return result }

    0 讨论(0)
  • 2020-12-24 11:48

    Easier one method:

    if ((x+y) % 2) {
        //statement
    }
    

    assuming of course that both variables are true booleans, that is, 1 or 0.

    • If x === y you'll get an even number, so XOR will be 0.
    • And if x !== y then you'll get an odd number, so XOR will be 1 :)

    A second option, if you notice that x != y evaluates as a XOR, then all you must do is

    if (x != y) {
        //statement
    }
    

    Which will just evaluate, again, as a XOR. (I like this much better)

    Of course, a nice idea would be to implement this into a function, but it's your choice only.

    Hope any of the two methods help someone! I mark this answer as community wiki, so it can be improved.

    0 讨论(0)
  • 2020-12-24 11:48

    Checkout this explanation of different implementations of XOR in javascript.

    Just to summarize a few of them right here:

    if( ( isEmptyString(firstStr) || isEmptyString(secondStr)) && !( isEmptyString(firstStr) && isEmptyString(secondStr)) ) {
       alert(SOME_VALIDATION_MSG); 
       return; 
    }
    

    OR

    if( isEmptyString(firstStr)? !isEmptyString(secondStr): isEmptyString(secondStr)) {
       alert(SOME_VALIDATION_MSG); 
       return;
    }
    

    OR

    if( (isEmptyString(firstStr) ? 1 : 0 ) ^ (isEmptyString(secondStr) ? 1 : 0 ) ) {
       alert(SOME_VALIDATION_MSG); 
       return;
    }
    

    OR

    if( !isEmptyString(firstStr)!= !isEmptyString(secondStr)) {
       alert(SOME_VALIDATION_MSG); 
       return;
    }
    
    0 讨论(0)
提交回复
热议问题