> ## 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# Logical AND

The `&` operator in C# functions as either a logical AND operator for boolean expressions or a bitwise AND operator for integral numeric types. Unlike the conditional logical AND operator (`&&`), the `&` operator enforces strict evaluation, meaning it always evaluates both operands regardless of the first operand's resulting value.

## Logical AND (Boolean Operands)

When applied to `bool` operands, the `&` operator computes the logical AND of its operands. The result is `true` if and only if both operands evaluate to `true`. Because it lacks short-circuiting behavior, any side effects present in the right-hand operand will always execute.

```csharp theme={"dark"}
bool result = leftOperand & rightOperand;
```

## Nullable Boolean Logical AND

When applied to nullable boolean (`bool?`) operands, the `&` operator implements three-valued logic. The operation evaluates to `false` if either operand is `false`, even if the other operand is `null`. If both operands are `true`, the result is `true`. In all other combinations involving `null`, the result is `null`. When applying the operator to two `null` literals, at least one operand must be explicitly cast to `bool?` to resolve compiler ambiguity (CS0034).

```csharp theme={"dark"}
bool? result1 = false & null;       // Evaluates to false
bool? result2 = true & null;        // Evaluates to null
bool? result3 = (bool?)null & null; // Evaluates to null
```

## Bitwise AND (Integral Operands)

When applied to integral numeric types (`sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `nint`, `nuint`) or enumeration types (`enum`), the `&` operator performs a bit-by-bit logical AND operation. It compares the binary representation of both operands. A bit in the result is set to `1` if and only if the corresponding bits in both operands are `1`; otherwise, the resulting bit is `0`.

```csharp theme={"dark"}
int result = numericOperand1 & numericOperand2;
```

## Type Promotion and Evaluation

For integral types smaller than `int` (such as `byte` or `short`), C# applies implicit numeric promotion before performing the bitwise operation. The operands are converted to `int` (or `uint`, depending on the types), the bitwise AND is performed at the 32-bit level, and the return type of the operation is `int`. Explicit casting is required to assign the result back to the smaller integral type.

```csharp theme={"dark"}
byte a = 0b_1111_0000;
byte b = 0b_1010_1010;
byte result = (byte)(a & b); // Explicit cast required because (a & b) returns int
```

## Compound Assignment

The `&` operator can be combined with the assignment operator (`=`) to form the compound assignment operator `&=`. This evaluates the `&` operation and assigns the result back to the left-hand variable. For integral types smaller than `int`, the compound assignment operator automatically applies an explicit cast back to the target type, preventing the `CS0266` compiler error that would occur with standard assignment due to implicit numeric promotion.

```csharp theme={"dark"}
variable &= operand; 
// Semantically equivalent to: variable = (T)(variable & operand);
// where T is the type of 'variable'.
```

## Operator Overloading

User-defined types (classes and structs) can overload the `&` operator to define custom behavior for instances of that type. When a binary operator is overloaded, the corresponding compound assignment operator (if any) is implicitly overloaded.

```csharp theme={"dark"}
public static CustomType operator &(CustomType left, CustomType right)
{
    // Implementation logic
    return new CustomType();
}
```

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