What’s the Difference Between & and && in C# ?

My previous article “Non Short Circuiting C# Conditional Operators” garnered some comments which suggested to me that I should expand on it. There can sometimes be some confusion over the & and && operator in C# so hopefully this article goes some way to helping clarify the differences between them.

The && Operator

Works with: bools

The && operator performs a logical AND on Boolean values.

For example, the following code:

bool b1 = true;
bool b2 = false;

bool isBothTrue = b1 && b2;

Console.WriteLine(isBothTrue);

Outputs:

False

If we get our two bool values from the result of a couple of methods instead, and the first method returns false, the && operator won’t execute the second method. The following code:

void Main()
{
    bool isBothTrue = Method1() && Method2();
    
    Console.WriteLine("Result: " + isBothTrue);
}

bool Method1()
{
    Console.WriteLine("Method 1 called - returning false");
    return false;
}

bool Method2()
{
    Console.WriteLine("Method 2 called - returning true");
    return true;
}

 

Results in this output:

Method 1 called - returning false
Result: False

Notice that there’s no “Method 2 called - returning true” line. This is because the && operator “short-circuits”; once it has determined the outcome (in this case the overall AND can never be true because the first Method1 is false) it stops evaluating the rest of the terms. This means Method2 doesn’t need to be executed. If Method1 returned true then Method2 would be executed as the overall result of the AND could still be either true or false.

The & Operator

Works with: bools or integral types

What the & operator does depends on the context in which it’s used.

MSDN: “For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands”.

Using & With Boolean Operands

If we use the & operator with Boolean operands, it performs a logical AND, much like the && operator. The difference is that & does not “short-circuit”. Even if Method1 returns false, Method2 will still be executed. The following code (notice the & instead of the &&):

void Main()
{
    bool isBothTrue = Method1() & Method2();
    
    Console.WriteLine("Result: " + isBothTrue);
}

bool Method1()
{
    Console.WriteLine("Method 1 called - returning false");
    return false;
}

bool Method2()
{
    Console.WriteLine("Method 2 called - returning true");
    return true;
}

Results in this output:

Method 1 called - returning false
Method 2 called - returning true
Result: False

Even though Method1 returned false, Method2 was still executed.

Using & With Integral Operands

When the & operator is used with integral operands (for example two ints) it computes a logical bitwise AND of its operands.

“For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands”

The following code:

void Main()
{
    int a = 0xF0;
    int b = 0xFF;

    int result = a & b;
    
    OutputBinary(a);
    OutputBinary(b);
    
    Console.WriteLine("Each bit gets AND-ed");
    
    OutputBinary(result);
    
    Console.WriteLine("As int: " + result);
}

void OutputBinary(int i)
{
    Console.WriteLine(Convert.ToString(i,2));
}

Produces this output:

11110000
11111111
Each bit gets AND-ed
11110000
As int: 240

Here, each bit in a gets AND-ed with it’s corresponding bit in b to produce the final bitwise AND-ed result of: 11110000 which is 240.

So when we talk about the & operator we can refer to it in the context of bools, whereby we’re talking about a logical AND of Boolean values; or in the context of integral types whereby it performs a bitwise AND of the bits of the values. Furthermore, when we refer to it in the context of bools we’re talking about a short-circuiting version of the && operator.

If you want to fill in the gaps in your C# knowledge be sure to check out my C# Tips and Traps training course from Pluralsight – get started with a free trial.

SHARE:

Comments (1) -

  • James Curran

    9/26/2013 12:16:33 AM | Reply

    The distinction is probably more important in C & C++ (and maybe Java -- it's been a while), where an int can be used interchangeably with a bool  (==0 is false, !=0 is true.)

    Hence you'd see things like:
          if (shouldPrint && ItemsToPrint.Count())
    (ie, print if we're been told to print AND we have something to print)

    Now, if you use the arithmetic AND, we can have a problem:
          if (shouldPrint & ItemsToPrint.Count())

    Here, instead of the int being converted to a bool, the bool is converted to an int (false is 0, true is 1), so if ItemsToPrint.Count() is an even number (say 4), then 1 & 4 = 0, which is treated as false.

    C# doesn't allow the implicit int to bool conversion,  so one would need to do something like:
          if (shouldPrint && ItemsToPrint.Count() > 0)

    which can be safely does with either the arithmetic or logical AND.


Add comment

Loading