Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The & operator in C# is a binary operator that performs a bitwise logical AND operation on integral numeric types and enumeration types, and an unconditional logical AND operation on boolean types.

Bitwise AND (Integral Types)

When applied to integral numeric types (int, uint, long, ulong, nint, nuint), the & operator evaluates the binary representation of both operands and compares them bit by bit. The resulting bit is set to 1 if and only if the corresponding bits in both operands are 1. Otherwise, the resulting bit is set to 0.
uint a = 0b_1100_1001; // Decimal: 201
uint b = 0b_1010_1100; // Decimal: 172
uint c = a & b;        // Result:  0b_1000_1000 (Decimal: 136)

Implicit Numeric Promotion

C# does not define built-in & operators that return smaller integral types (byte, sbyte, short, ushort, char). When the & operator is applied to these types, C# applies implicit numeric promotion. The operands are automatically promoted to int, and the result of the operation is an int. If one operand is uint and the other is a signed integral type (sbyte, short, or int), both operands are promoted to long, and the result is a long.
byte x = 0b_1010;
byte y = 0b_1100;

// byte z = x & y; // Compiler error: Cannot implicitly convert type 'int' to 'byte'
int result = x & y; 
byte explicitResult = (byte)(x & y); // Requires an explicit cast

uint unsignedInt = 5;
int signedInt = -1;
long mixedResult = unsignedInt & signedInt; // Promoted to long

Bitwise AND (Enumeration Types)

The & operator is fully supported for enum types. The operation is performed on the underlying integral type of the enumeration. This is mechanically essential for evaluating bitmask enumerations, typically decorated with the [Flags] attribute.
[Flags]
public enum FileAccess
{
    None = 0,
    Read = 1,
    Write = 2,
    Execute = 4
}

FileAccess permissions = FileAccess.Read | FileAccess.Write;
FileAccess check = permissions & FileAccess.Read; // Evaluates to FileAccess.Read

Unconditional Logical AND (Boolean Types)

When applied to bool operands, the & operator computes the logical AND. The result is true if both operands evaluate to true; otherwise, the result is false. Unlike the conditional logical AND operator (&&), the & operator is non-short-circuiting. It strictly evaluates both the left-hand side (LHS) and right-hand side (RHS) expressions, regardless of the outcome of the LHS evaluation.
bool EvaluateRHS()
{
    Console.WriteLine("RHS Evaluated");
    return true;
}

// The LHS is false, but EvaluateRHS() is still invoked.
bool result = false & EvaluateRHS(); 

Nullable Boolean Logical AND (bool?)

When applied to nullable boolean (bool?) operands, the & operator implements three-valued logic. The evaluation rules are:
  • Returns true if both operands are true.
  • Returns false if either operand is false, even if the other operand is null.
  • Returns null otherwise (i.e., if one operand is true and the other is null, or if both are null).
The compiler requires explicit typing for null literals in these expressions to resolve the operator overload.
bool? nullBool = null;

bool? a = true & nullBool;     // Evaluates to null
bool? b = false & nullBool;    // Evaluates to false
bool? c = nullBool & nullBool; // Evaluates to null

// Alternatively, using explicit casts:
bool? d = true & (bool?)null;  // Evaluates to null

Compound Assignment

The & operator supports compound assignment via the &= operator. An expression using &= evaluates the bitwise or logical AND of the operands and assigns the result to the left operand.
int x = 0b_1111;
x &= 0b_1010; // x is now 0b_1010

bool flag = true;
flag &= false; // flag is now false

Operator Overloading

User-defined types (classes and structs) can overload the & operator to define custom evaluation logic. When a user-defined type overloads the & operator, the &= operator is implicitly overloaded as well.
public struct CustomType
{
    public int Value { get; }

    public CustomType(int value) => Value = value;

    public static CustomType operator &(CustomType left, CustomType right)
    {
        return new CustomType(left.Value & right.Value);
    }
}
Master C# with Deep Grasping Methodology!Learn More