【发布时间】:2016-11-25 23:48:59
【问题描述】:
对于下面的示例代码:
// Note that a, b, c and d can have value of 0 or 1 only
int isAllTrue(int a, int b, int c, int d)
{
return (a && b && c && d);
// THIS ALSO RETURNS CORRECT VALUE
// return (a & b & c & d);
}
如果我们知道操作数可以是 0 或 1,从性能的角度来看,哪种操作更可取:按位还是逻辑?真的很重要吗?
当操作数为0时,按位“&”是否停止计算?
【问题讨论】:
-
看看[ this ]的答案。
-
(10 & 5 & 3) = 0,(10 && 5 && 3) = 1所以'bitwise'和'conditional and'表达式不能互换 . -
@DavidC.Rankin 问题是关于已知操作数为 0 或 1 的情况。
-
@tbetous StackOverflow 以标签为中心。无法使用不同语言的标签关闭重复项。因此,在将来提出作为重复问题结束时请尊重这一点
标签: c bit-manipulation