& Operator (C# Reference)

The & operator is supported in two forms: a unary address-of operator or a binary logical operator.

Unary address-of operator

The unary & operator returns the address of its operand. For more information, see How to: obtain the address of a variable.

The address-of operator & requires unsafe context.

Integer logical bitwise AND operator

For integer types, the & operator computes the logical bitwise AND of its operands:

uint a = 0b_1111_1000;
uint b = 0b_1001_1111;
uint c = a & b;
Console.WriteLine(Convert.ToString(c, toBase: 2));
// Output:
// 10011000

Note

The preceding example uses the binary literals introduced in C# 7.0 and enhanced in C# 7.2.

Because operations on integer types are generally allowed on enumeration types, the & operator also supports enum operands.

Boolean logical AND operator

For bool operands, the & operator computes the logical AND of its operands. The result of x & y is true if both x and y are true. Otherwise, the result is false.

The & operator evaluates both operands even if the first operand evaluates to false, so that the result must be false regardless of the value of the second operand. The following example demonstrates that behavior:

bool SecondOperand() 
{
    Console.WriteLine("Second operand is evaluated.");
    return true;
}

bool test = false & SecondOperand();
Console.WriteLine(test);
// Output:
// Second operand is evaluated.
// False

The conditional AND operator && also computes the logical AND of its operands, but evaluates the second operand only if the first operand evaluates to true.

For nullable bool operands, the behavior of the & operator is consistent with SQL's three-valued logic. For more information, see the The bool? type section of the Using nullable types article.

Operator overloadability

User-defined types can overload the binary & operator. When a binary & operator is overloaded, the AND assignment operator &= is also implicitly overloaded.

C# language specification

For more information, see The address-of operator and Logical operators sections of the C# language specification.

See also