替换“==”与按位运算符
仅使用按位运算符(|、&、~、^、>>、<<)和其他基本运算符(如+、-、!),是否可以替换下面的“==”?
int equal(int x, int y) {
return x == y;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请记住,
XOR
与NOT EQUALS
完全相同,XNOR
与EQUALS
完全相同。因此,以下内容将为您提供您想要的:Remember that an
XOR
is the exactly same asNOT EQUALS
andXNOR
is exactly the same asEQUALS
. So, the following will give you exactly what you want:如果两个数字之间没有差异,则它们相等:
Two numbers are equal if there is no difference between them:
C
!
运算符实际上只是!= 0
的简写,因此使用它似乎非常接近作弊:)这是我仅使用按位运算的看法,假设是 32 位带有算术右移的二进制补码机器(从技术上讲,在 C 算术右移中未定义,但我在二进制补码机器上见过的每个 C 编译器都正确支持这一点):
也就是说,实际的编译器不存在此问题。真实的硬件实际上可以直接支持比较。详细信息取决于体系结构,但有两种基本模型:
更多类似 RISC 的平台通常具有直接的“相等则分支”和“小于则分支”操作数,它们根据结果进行比较和分支。它基本上相当于C代码
或
全在一个机器指令中。
The C
!
operator is really just shorthand for!= 0
, so using it seems very close to cheating :)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):
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. It's basically equivalent to the C code
or
all in one machine instruction.
这个例子与减法相同,但更明确地说明了某些架构如何进行寄存器比较(我相信像 ARM)。
1 表示输入 ALU 的进位位。一个数字
x
按位求补。取补码并加 1 产生该数字的二进制补码(x
变为-x
),然后将其与另一个数字相加,得到差值以确定相等。因此,如果两个数字相等,您将得到
-x + x => 0 。
(在寄存器级别,
!
运算符尚未完成,您只需测试条件代码或标志寄存器的“零位”,如果寄存器操作产生零结果,则该位被设置,否则很清楚。)This example is the same as subtraction, but is more explicit as to how some architectures do register comparison (like the ARM, I believe).
The 1 signifies the carry-bit input into the ALU. One number
x
is bitwise complemented. 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.So if both numbers are equal, you get
-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.)由于 XOR 与 (!=) 相同,因此 (x ^ y) 仅当值相等时才会返回 0。
我的看法如下,因为它是明智的,使用按位运算符并且有效。
As XOR is same as (!=), hence (x ^ y) will return 0 only for equal values.
My take is the following because it is sensible, uses bit-wise operator and working.
我对此
解释的看法:如果
x == y
,则x & ~y
计算结果为0
返回 1,否则返回 0 作为x!=y
。上面的代码在某些情况下会失败,最高有效位变为 1。解决方案是添加 1。即正确答案是
My Take on this
Explanation: If
x == y
, thenx & ~y
evaluates to0
return 1, else return 0 asx!=y
.The above code fails in certain cases where the Most significant bit turns to 1. The solution is to add a 1. i.e correct answer is