Hap*_*mad 15 c# bit-manipulation
什么是简短,优雅,按位的方式来编写这个C#代码的最后一行而不写b两次:
bool getAsIs = ....
bool b = ....
getAsIs ? b : !b
Run Code Online (Sandbox Code Playgroud)
drf*_*drf 32
真值表可表示为:
getAsIs b getAsIs ? b : !b
--------------------------------
0 0 1
0 1 0
1 0 0
1 1 1
Run Code Online (Sandbox Code Playgroud)
结果可表示为:
result = (getAsIs == b);
Run Code Online (Sandbox Code Playgroud)
尝试使用二进制XOR(^运算符(C#参考)):
bool getAsIs = true;
bool b = false;
bool result = !(getAsIs ^ b);
Run Code Online (Sandbox Code Playgroud)