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

The `&` operator in C# functions as both a unary operator that returns the memory address of its operand, and a binary operator that performs either a logical AND operation on boolean types or a bitwise AND operation on integral numeric types. Unlike the conditional logical AND operator (`&&`), the binary `&` operator is non-short-circuiting. It evaluates operands strictly from left to right and will evaluate the right-hand operand regardless of the left-hand operand's resulting boolean value, provided the left-hand evaluation does not halt execution by throwing an exception.

## Unary Address-Of Operator (`unsafe`)

When used as a unary operator (`&operand`), it acts as the address-of operator. It returns a pointer to the memory address of the variable it is applied to. This operation requires an `unsafe` context and the project must be compiled with the `/unsafe` compiler flag.

The operand must satisfy strict type and memory constraints:

1. **Unmanaged Type Constraint:** The operand must be of an *unmanaged type* (e.g., primitive numerics, enums, pointers, or structs containing only unmanaged fields). Attempting to take the address of a managed type (such as a `string` or a `class` reference) results in compiler error `CS0208`.
2. **Memory Location Constraint:** The operand must be a variable, an array element, or a field. It cannot be a constant, a value, or a standard property access (which are implemented as methods and lack a direct memory address, yielding `CS0211`). C# 7.0 and later does permit taking the address of a `ref`-returning property.
3. **Pinning Movable Variables:** If the operand is a "movable" variable residing on the managed heap (such as an array element or a field of a reference type), taking its address requires pinning it in memory using the `fixed` statement. This prevents the garbage collector from relocating the variable. Attempting to use `&` directly on an unfixed movable variable results in compiler error `CS0212`.

```csharp theme={"dark"}
using System;

// Requires compiling with the /unsafe flag
unsafe
{
    // Local variable (fixed variable, resides on the stack)
    int localNumber = 42;
    int* pointerToLocal = &localNumber; 
    
    // Movable variable (resides on the managed heap)
    int[] array = { 10, 20, 30 };
    
    // Must be pinned using 'fixed' to take the address
    fixed (int* pointerToArrayElement = &array[0])
    {
        Console.WriteLine(*pointerToArrayElement); // Outputs: 10
    }
}
```

## Binary Logical AND Evaluation (`bool`)

When both operands are of type `bool`, the binary `&` operator computes the logical AND. The result is `true` if and only if both operands evaluate to `true`; otherwise, the result is `false`. Because it lacks short-circuiting behavior, any assignments or side effects present in the right-hand operand are evaluated even if the left-hand operand evaluates to `false`.

```csharp theme={"dark"}
using System;

bool a = false;
bool b = false;

// The right-hand side (b = true) is evaluated even though 'a' is already false.
bool result = a & (b = true); 

Console.WriteLine(result); // Outputs: False
Console.WriteLine(b);      // Outputs: True
```

## Nullable Boolean Logical AND (`bool?`)

When applied to nullable booleans (`bool?`), the `&` operator implements three-valued logic. The operator evaluates to `false` if either operand is `false`, regardless of whether the other operand is `null`. If neither operand is `false` and at least one operand is `null`, the result is `null`. It evaluates to `true` only if both operands are `true`.

Because the `null` literal does not have an inherent type in C#, it must be explicitly cast or assigned to a `bool?` variable before applying the operator to avoid compiler error `CS0019`.

```csharp theme={"dark"}
using System;

bool? nullBool = null;

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

Console.WriteLine(a.HasValue); // Outputs: False
Console.WriteLine(b);          // Outputs: False
Console.WriteLine(c.HasValue); // Outputs: False
Console.WriteLine(d.HasValue); // Outputs: False
```

## Binary Bitwise AND Evaluation (Integral Types)

When applied to integral numeric types (`sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `nint`, `nuint`) or `char`, the `&` operator computes the bitwise AND of its operands. It evaluates the binary representation of the operands, comparing each bit at the corresponding position. The resulting bit is set to `1` if both corresponding bits in the operands are `1`; otherwise, the resulting bit is set to `0`.

```csharp theme={"dark"}
using System;

uint a = 0b_1100_1010; // Decimal: 202
uint b = 0b_1010_1100; // Decimal: 172

uint result = a & b;   // 0b_1000_1000 (Decimal: 136)

Console.WriteLine(result); // Outputs: 136
```

## Type Promotion and Evaluation Rules

* **Numeric Promotion:** C# applies binary numeric promotion to integral operands before performing the bitwise operation. Integral types smaller than `int` (such as `byte`, `sbyte`, `short`, `ushort`, and `char`) are always implicitly promoted to `int`, even if both operands are of the exact same type. For example, in the expression `byte & byte`, both operands are promoted to `int`, and the resulting value is an `int`, not a `byte`. If the operands are of different types, they are promoted to the smallest common type that can contain both (e.g., an `int` and a `long` are both promoted to `long`).
* **Enumerations:** The `&` operator is supported for enumeration (`enum`) types. The operation is performed at the bit level on the underlying integral values of the enumeration members. The return type is the enumeration type itself.
* **Operator Overloading:** User-defined types (`class` or `struct`) can overload the `&` operator to define custom behavior. When a type overloads the binary `&` operator, the compound assignment operator (`&=`) is implicitly overloaded as well. Critically, if a user-defined type `T` overloads the binary `&` operator such that it takes exactly two parameters of type `T` and returns type `T` (i.e., `public static T operator &(T x, T y)`), and the type also overloads the `true` and `false` operators, it implicitly enables support for the short-circuiting conditional logical AND operator (`&&`) for that specific type.

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