> ## 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.

# C# Bitwise AND Assignment

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`.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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`.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
[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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
