有人可以解釋這種行爲嗎?爲什麼不按位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