相关疑难解决方法(0)

为什么按位运算符不像逻辑"和\或"运算符那样智能

我只是注意到按位操作并不像逻辑"和\或"操作那样"智能",我想知道为什么?

这是一个例子:

// For the record
private bool getTrue(){return true;}
private bool getFalse(){return false;}

// Since a is true it wont enter getFalse.
bool a = getTrue() || getFalse(); 

// Since a is false it wont enter getTrue.
bool b = getFalse() && getTrue(); 

// Since b is false it wont enter getTrue.
b = b && getTrue();
Run Code Online (Sandbox Code Playgroud)

但是,按位运算符"| ="和"&="并不那么聪明:

bool a = getTrue();
a |= getFalse(); // a has no chance to get false but it still enters the …
Run Code Online (Sandbox Code Playgroud)

c# optimization bit-manipulation operators

5
推荐指数
2
解决办法
439
查看次数

标签 统计

bit-manipulation ×1

c# ×1

operators ×1

optimization ×1