Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 882
Chris Schiffhauer Avatar asked Jan 22 '26 15:01

Chris Schiffhauer


2 Answers

It is a logical operator on bools that evaluates both sides regardless of the value of the left side.

like image 164
Eric Lippert Avatar answered Jan 24 '26 04:01

Eric Lippert


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)

like image 44
Damien_The_Unbeliever Avatar answered Jan 24 '26 05:01

Damien_The_Unbeliever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!