How does Bitwise AND interact with boolean values?
I have come across a piece of code that I can't wrap my head around:
public void Connect()
{
if (!(!string.IsNullOrEmpty(_vpnConnectionName) & !string.IsNullOrEmpty(_ipToPing)))
{
return;
}
GetConnected();
}
I've researched the single ampersand on SO and elsewhere, and found it is a Bitwise AND operand. However the explanations all revolve around integers.
I can understand the applicability of bitwise operations on integers, but how are they applicable to booleans?
它是一个逻辑运算符,用于评估两侧的bools,而不考虑左侧的值。
For bool
, &
isn't a bitwise AND
operator - it's the non-short-circuiting logical AND
operator - it always evaluates both arguments - as opposed to the "usual" &&
operator that only evaluates its second argument if its first argument is true
.
(Realised that it may not be obvious, that the first &
above is linked to the actual documentation)
上一篇: =和&=
下一篇: 按位与与布尔值进行交互的方式如何?