&& vs & and | vs ||… What’s the difference?

color-01 It seems like such a trivial thing to be talking about but not knowing the difference between the two operators can make a huge difference between working code and code that only seems to work.

Let me illustrate:

        bool b = false;
        bool c = true;
        if(b & c)
            // do something

        if(b && c)
            // do something

In the code above, both b & c and b && c evaluate to false, so  we are safe.  No problems.  But this leads us to believe that the following code is also safe:

        string s = null;
        if(s != null & s.Length > 0)
            // do something

        if(s != null && s.Length > 0)
            // do something

and this is what would get you in trouble.

The single ampersand and single pipe are knows as “bitwise operators.”  What this means in practical terms is that they will take whatever they find on BOTH sides of the operator and AND (&) or OR(|) them together.

So in the case of:

if(s != null & s.Length > 0)

s.Length > 0 will still get evaluated even if s is null.  Not exactly what we had in mind.

There is another side effect of bitwise operators that gets used very infrequently.  Because they are bitwise, I can use them on integers as well as boolean values.

int i= 5 & 6;

is a perfectly legal construct in CSharp.  It will AND all the bits that make up 5  (101) and all the bits that make up 4 (100) and store the result in i (4 or 100).

On the other hand, && and || are strictly used for boolean expressions and will evaluate as little of the expression as they can get away with.

This is why we can write:

        if(s != null && s.Length > 0)
            // do something

If s is null, we already know that the expression will fail, so there is no need to evaluate the string’s length.

And now for one of my favorite binary statements.

There are 10 types of people in the world.  Those who understand binary and those who don’t.

 


Other post in Advanced CSharp

Other Related Items:

Aeromax 6-8 yrs. Jr. Train Engineer SuitAeromax 6-8 yrs. Jr. Train Engineer SuitAll Aboard..Is what they'll hear when dressed the part of the Train Engineer. This outfit has the kids dressed from head to toe just the way we all remember it. Includes: Adjustable engineer's hat Red bandana Full length adjustable bib overalls Matching quality leather gloves Size 6/8 for ages 6 8
Peanut Butter & Co. Peanut Butter, Smooth Operator, 16-Ounce Jars (Pack of 6)Peanut Butter & Co. Peanut Butter, Smooth Operator, 16-Ounce Jars (Pack of 6)Our great-tasting all-natural peanut butter just got even better! We've reformulated Smooth Operator so that it is now in the No-Stir Natural style. W... Read More >
Other End of the Line (Widescreen)Other End of the Line (Widescreen)Studio: Tcfhe/mgm Release Date: 03/31/2009 Rating: Pg13

Related Post

2 Responses to “&& vs & and | vs ||… What’s the difference?”

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor