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 is a compound assignment operator that performs a bitwise AND or logical AND operation between its left-hand and right-hand operands, subsequently assigning the resulting value to the left-hand operand. The expression x &= y is generally evaluated as x = x & y, subject to two strict technical distinctions:
  1. The left-hand operand x is evaluated only once.
  2. For integral types smaller than int (such as byte, sbyte, short, ushort, and char), the binary & operator applies numeric promotion, converting the operands to int. The &= operator automatically performs a hidden explicit cast back to the original type T (evaluating as x = (T)(x & y)) only if y is implicitly convertible to T. If y is not implicitly convertible to T (for example, if y is an int variable), the compiler emits error CS0266 rather than automatically casting.

Type-Specific Behavior

The underlying operation of the &= operator depends strictly on the data types of its operands:

1. Integral Types

When applied to integral numeric types (int, uint, long, ulong, short, ushort, byte, sbyte, char, nint, nuint), the operator performs a bitwise AND. It compares the binary representations of both operands bit by bit. A bit in the result is set to 1 if and only if the corresponding bits in both operands are 1; otherwise, it is set to 0.
int a = 12;      // Binary: 0000 1100
a &= 10;         // Binary: 0000 1010
                 // Result: 0000 1000
// a is now 8

byte b = 255;    
b &= 15;         // Compiles: 15 is a constant implicitly convertible to byte. Evaluated as b = (byte)(b & 15)

int i = 15;
// b &= i;       // Error CS0266: Cannot implicitly convert type 'int' to 'byte'.

2. Boolean Types

When applied to bool operands, the operator performs a logical AND. It evaluates to true if and only if both operands are true. Unlike the && operator, the & operation within &= is non-short-circuiting; the right-hand operand is always evaluated regardless of the left-hand operand’s state.
bool flag = true;
flag &= false; 
// flag is now false

bool state = false;
state &= (MethodThatReturnsBool()); 
// MethodThatReturnsBool() is executed even though state is already false

3. Nullable Value Types

The &= operator supports lifted operators for nullable value types (T?). For nullable integral types, the operation evaluates to null if either operand is null. For the nullable boolean type (bool?), the &= operator applies three-valued logic, adhering to SQL-like boolean semantics:
  • false &= null evaluates to false (because false & <any> is always false).
  • true &= null evaluates to null.
  • null &= null evaluates to null.
bool? b1 = true;
b1 &= null;
// b1 is now null

bool? b2 = false;
b2 &= null;
// b2 is now false

4. Enumerations

When applied to enum types, the operator performs a bitwise AND operation on the underlying integral values of the enumeration members.
[Flags]
public enum FileAccess
{
    None = 0,
    Read = 1,
    Write = 2,
    Execute = 4
}

FileAccess access = FileAccess.Read | FileAccess.Write; // Value: 3 (Binary: 0011)
access &= FileAccess.Read;                              // Value: 1 (Binary: 0001)
// access is now FileAccess.Read

Operator Overloading

The &= operator cannot be explicitly overloaded. However, if a user-defined type overloads the binary & operator, the compound assignment operator &= is implicitly evaluated using that overload.
Master C# with Deep Grasping Methodology!Learn More