检查两个整数是否具有相同的符号

检查两个整数是否具有相同的符号

问题描述:

我正在寻找一种有效的方法来检查两个数字是否具有相同的符号。

I'm searching for an efficient way to check if two numbers have the same sign.

基本上我正在寻找比这更优雅的方式:

Basically I'm searching for a more elegant way than this:

var n1 = 1;
var n2 = -1;

( (n1 > 0 && n2 > 0) || (n1<0 && n2 < 0) )? console.log("equal sign"):console.log("different sign");

使用按位运算符的解决方案也可以。

A solution with bitwise operators would be fine too.

代码字符较少,但可能会溢出:

Fewer characters of code, but might overflow:

n1*n2 > 0 ? console.log("equal sign") : console.log("different sign or zero");

或没有整数溢出,但略大:

or without integer overflow, but slightly larger:

(n1>0) == (n2>0) ? console.log("equal sign") : console.log("different sign");

如果您认为0为正,则>应替换为<

if you consider 0 as positive the > should be replaced with <