Skip to main content
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.

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.

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.

4. Enumerations

When applied to enum types, the operator performs a bitwise AND operation on the underlying integral values of the enumeration members.

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.
Tired of Poor C# Skills? Fix That With Deep Grasping!Learn More