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

The `|` operator in C# functions as a bitwise OR operator for integral and enumeration types, and as a non-short-circuiting logical OR operator for boolean types. Its behavior is determined by the type of its operands, and it can be customized for user-defined types via operator overloading.

## Bitwise OR (Integral Types)

When applied to integral numeric types (`int`, `uint`, `long`, `ulong`, `short`, `ushort`, `byte`, `sbyte`, `nint`, `nuint`), the `|` operator performs a bit-by-bit logical OR operation. It compares each bit of the first operand to the corresponding bit of the second operand. If either bit is `1`, the resulting bit is set to `1`; if both are `0`, the resulting bit is `0`.

**Numeric Promotion:** When applying the `|` operator to smaller integral types (`byte`, `sbyte`, `short`, `ushort`), C# applies implicit numeric promotion. The operands are converted to `int`, and the operation evaluates to an `int`. Assigning the result back to the smaller type requires an explicit cast.

```csharp theme={"dark"}
int a = 5;          // Binary: 0000 0101
int b = 3;          // Binary: 0000 0011
int result = a | b; // Binary: 0000 0111 (Decimal: 7)

byte c = 1;
byte d = 2;
// Numeric promotion requires an explicit cast to assign back to byte
byte byteResult = (byte)(c | d); 
```

## Logical OR (Boolean Types)

When applied to `bool` operands, the `|` operator computes the logical OR of the expressions. It returns `true` if at least one operand evaluates to `true`; otherwise, it returns `false`.

Unlike the conditional logical OR operator (`||`), the `|` operator guarantees **strict evaluation**. It evaluates both the left-hand side and right-hand side operands regardless of the left-hand side's result, meaning it does not short-circuit.

**Nullable Boolean Types:** For `bool?` operands, the `|` operator implements three-valued logic. If either operand is `true`, the result is `true`. If one operand is `false` and the other is `null`, the result is `null`.

```csharp theme={"dark"}
bool lhs = true;
bool rhs = false;
bool strictResult = lhs | rhs; // Evaluates to true, both sides are evaluated

// Three-valued logic with bool?
bool? nullBool = null;
bool? trueResult = true | nullBool;   // Evaluates to true
bool? nullResult = false | nullBool;  // Evaluates to null
```

## Enumeration Types

When applied to operands of the same `enum` type, the `|` operator performs a bitwise OR operation on the underlying integral values of the enumeration members. This is mechanically identical to the integral bitwise OR but maintains the strong typing of the enumeration.

```csharp theme={"dark"}
[Flags]
public enum FileAccess
{
    None = 0,      // 0000
    Read = 1,      // 0001
    Write = 2,     // 0010
    Execute = 4    // 0100
}

FileAccess access = FileAccess.Read | FileAccess.Write; 
// Underlying math: 1 | 2 = 3 (Binary 0011)
```

## Operator Overloading

The `|` operator can be overloaded to provide custom behavior for user-defined types (classes and structs). When a user-defined type overloads the `|` operator, it defines the specific return type and evaluation logic for instances of that type.

```csharp theme={"dark"}
public struct CustomBitset
{
    public int Value { get; }
    public CustomBitset(int value) => Value = value;

    public static CustomBitset operator |(CustomBitset left, CustomBitset right)
    {
        return new CustomBitset(left.Value | right.Value);
    }
}

CustomBitset set1 = new CustomBitset(4);
CustomBitset set2 = new CustomBitset(8);
CustomBitset combined = set1 | set2;
```

## Compound Assignment

The `|` operator can be combined with the assignment operator to form the `|=` compound assignment operator. The expression `x |= y` is evaluated as `x = x | y`, except that `x` is only evaluated once.

```csharp theme={"dark"}
int mask = 8;       // Binary: 1000
mask |= 4;          // Binary: 1100 (Decimal: 12)

bool flag = false;
flag |= true;       // Evaluates to true
```

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