如何在javascript中计算XOR运算? [英] How to compute XOR operation in javascript?
本文介绍了如何在javascript中计算XOR运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正致力于计算Xor操作,但我无法准确计算出正确的结果。请帮我找一个解决方案。
如果我们真和假,如何计算XOR运算以获得最终结果?
问候,
Amal Raj
我的尝试:
我尝试过:
result = s1 ^ s2; //这里s1 =true,s2 =false
Hi All,
I am working on computing Xor operation, but i could not exactly compute the proper result. Please help me to find a solution.
If we "true" and "false", how to compute XOR operation to get the final result?
Regards,
Amal Raj
What I have tried:
I have tried like:
result = s1 ^ s2; // here s1= "true", s2= "false"
推荐答案
问题是,你使用的是Boolean
值为字符串[true
,false
]而不是[true
,false
]
如果您使用string
值您将结果
全部组合为0
尝试将其解析为boolean
值和XOR
它
The problem is, you are using theBoolean
values as string ["true"
,"false"
] instead of [true
,false
]
if you usestring
values you will theresult
as0
in all the combination
try parsing it to aboolean
value andXOR
it
var s1 = "true", s2 = "false";
var s1Bool = s1 === "true"; // true
var s2Bool = s2 === "true"; // false
result = s1Bool ^ s2Bool; // 1
^是JS XOR,它的工作原理是:
^ is the JS XOR, and it works:
<!DOCTYPE html>
<html>
<body>
<p>This example calls a function which performs a calculation, and returns the result:</p>
<p id="demo"></p>
<script>
function doXOR(a,b) {
var result = a ^ b;
return result;
}
document.getElementById("demo").innerHTML = doXOR(true, true);
document.getElementById("demo").innerHTML += doXOR(true, false);
document.getElementById("demo").innerHTML += doXOR(false, false);
document.getElementById("demo").innerHTML += doXOR(false, true);
</script>
</body>
</html>
为我执行时给出0101 ...我检查你的代码是否正在执行。
It gives 0101 when executed for me...I'd check your code is executing.
你在这里已经成为一名成员了解得比发表同一个问题 [ ^ ]。
You have been a member here long enough to know better than to post the same question[^] in multiple forums.
这篇关于如何在javascript中计算XOR运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文