[英]Replacing “==” with bitwise operators
Using only bitwise operators (|, &, ~, ^, >>, <<) and other basic operators like +, -, and !, is it possible to replace the "==" below? 仅使用按位运算符(|,&,〜,^,>>,<<)和其他基本运算符(如+, - 和!),是否可以替换下面的“==”?
int equal(int x, int y) {
return x == y;
}
Remember that an XOR
is the exactly same as NOT EQUALS
and XNOR
is exactly the same as EQUALS
. 请记住,
XOR
与NOT EQUALS
完全相同, XNOR
与EQUALS
完全相同。 So, the following will give you exactly what you want: 因此,以下内容将为您提供您想要的内容:
return !(x ^ y);
Two numbers are equal if there is no difference between them: 如果它们之间没有差异,则两个数字相等:
int equal(int x, int y){
return !(x-y);
}
The C !
C
!
operator is really just shorthand for != 0
, so using it seems very close to cheating :) 运算符实际上只是
!= 0
简写,所以使用它似乎非常接近欺骗:)
Here's my take just using bitwise operations, assuming a 32-bit two's complement machine with arithmetic right shifts (technically, in C arithmetic right shifts are undefined, but every C compiler I've ever seen on a two's complement machine supports this correctly): 这是我使用按位运算的假设,假设32位二进制补码机具有算术右移(技术上,在C算术右移是未定义的,但我在双补码机上看到的每个C编译器都支持这一点):
int t = (x - y) | (y - x); // <0 iff x != y, 0 otherwise
t >>= 31; // -1 iff x != y, 0 otherwise
return 1 + t; // 0 iff x != y, 1 otherwise
That said, actual compilers don't have this problem. 也就是说,实际的编译器没有这个问题。 Real hardware actually has direct support for comparisons.
真正的硬件实际上直接支持比较。 The details depend on the architecture, but there's two basic models:
细节取决于架构,但有两个基本模型:
More RISC-like platforms typically have direct "branch if equal" and "branch if less than" operands that do a comparison and branch based on the result. 更多类似RISC的平台通常具有直接“分支如果相等”和“分支如果小于”操作数,它们根据结果进行比较和分支。 It's basically equivalent to the C code
它基本上等同于C代码
if (a == b) goto label;
or 要么
if (a < b) goto label;
all in one machine instruction. 一体机指令。
This example is the same as subtraction, but is more explicit as to how some architectures do register comparison (like the ARM, I believe). 这个例子和减法一样,但更明确一些架构如何进行寄存器比较(比如ARM,我相信)。
return !(1 + ~x + y);
The 1 signifies the carry-bit input into the ALU. 1表示进入ALU的进位位输入。 One number
x
is bitwise complemented. 一个数字
x
按位补充。 Taking the complement and adding 1 produces the two's complement of the number ( x
becomes -x
), and then it's added to the other number to get the difference to determine equality. 取补码并加1会产生数字的二进制补码(
x
变为-x
),然后将其添加到另一个数字以获得差异以确定相等性。
So if both numbers are equal, you get -x + x => 0
. 因此,如果两个数字相等,则得到
-x + x => 0
。
(On a register level the !
operator isn't done, and you just test the "zero bit" of the condition codes or flags register, which gets set if the register operation produces a result of zero, and is clear otherwise.) (在寄存器级别上,
!
运算符未完成,您只需测试条件代码或标志寄存器的“零位”,如果寄存器操作产生的结果为零,则该寄存器将被设置,否则将被清除。)
As XOR is same as (!=), hence (x ^ y) will return 0 only for equal values. 由于XOR与(!=)相同,因此(x ^ y)仅对相等的值返回0。 My take is the following because it is sensible, uses bit-wise operator and working.
我的观点如下,因为它是明智的,使用逐位运算符和工作。
int notEqual(int x, int y){
return (x ^ y);
}
My Take on this 我接受这个
int equal(int x, int y){
if((x & ~y) == 0)
return 1;
else
return 0;
}
Explanation: If x == y
, then x & ~y
evaluates to 0
return 1, else return 0 as x!=y
. 说明:如果
x == y
,则x & ~y
求值为0
返回1,否则返回0为x!=y
。
Edit1: The above is equivalent to
int equal(int x, int y){
return !(x & ~y) ; // returns 1 if equal , 0 otherwise.
}
The above code fails in certain cases where the Most significant bit turns to 1. The solution is to add a 1. ie correct answer is 在最高有效位变为1的某些情况下,上述代码失败。解决方案是添加1.即正确的答案是
return !(x & (~y +1) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.