有人可以解释这种行为吗?为什么不按位xor在if语句中工作?
https://jsfiddle.net/td1qtyxL/9/
function checkSignsWeird(a,b){
var output = "";
if(a^b < 0){
output = "The "+a+" and "+b+" have DIFFERENT signs.";
}else{
output = "The "+a+" and "+b+" have the SAME sign.";
}
console.log(output);
}
基本上除非a^b
被存储在一个变量(或缠绕在括号中),这是行不通的。
checkSignsWeird(-50,40);
checkSignsWeird(60,70);
两者都产生相同的结果。
艾米我做错了什么或这是一个错误?当if语句处于if语句或其他地方时,它们的工作方式会有所不同吗?我不经常按位工作,只是认为这是优雅的,从这里开始回答跟进:Check if two integers have the same sign
请指定您的编程语言。它看起来像JS –
对不起,我忘了:) – Firsh
也许[*运算符优先级*](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)? – RobG