Is there anyway to implement XOR in javascript

前端 未结 18 686
孤城傲影
孤城傲影 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条回答
  •  Happy的楠姐
    2020-12-24 11:34

    You are doing an XOR of boolean values which is easy to model into a bitwise XOR (which Javascript has):

    var a = isEmptyString(firstStr) ? 1 : 0;
    var b = isEmptyString(secondStr) ? 1 : 0;
    
    if(a ^ b) { ... }
    

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

提交回复
热议问题