{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0f5301b0",
   "metadata": {},
   "source": [
    "# Operators\n",
    "\n",
    "Operators in programming languages are symbols that tells the\n",
    "compiler or interpreter to perform specific operations. The\n",
    "operand combined with the operator makes an operation. In\n",
    "an operation, an *operand* is the data that is being operated on.\n",
    "The position of the operator, with respect to its\n",
    "operands, may be prefix, infix or postfix.\n",
    "\n",
    "Common simple operators include\n",
    "\n",
    "- *arithmetic* (e.g. addition with +),\n",
    "- *comparison* (relational) (e.g. \"greater than\" with >),\n",
    "- *logical* (e.g. AND, also written && in some languages), and\n",
    "- *assignment* (e.g., =, +=) operators.\n",
    "\n",
    "Additional types of operators include assignment (usually = or :=),\n",
    "field access in a record or object (usually .), and bitwise and\n",
    "shift operators.\n",
    "\n",
    "Operators can be categorized based on the number of operands\n",
    "required. The three categories of operators based on the\n",
    "number of operands they require are:\n",
    "\n",
    "- Unary operators: which require one operand (e.g., ++, !)\n",
    "- Binary operators: which require two operands (e.g., +)\n",
    "- Ternary operators: which require three operands (e.g., condition ? consequent : alternative)\n",
    "\n",
    "```{index} arithmetic operators\n",
    "```\n",
    "\n",
    "(arithmetic-operators)=\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfb6b504",
   "metadata": {},
   "source": [
    "The table below summarizes key C# operators.\n",
    "\n",
    "| Category | Operators | Description | Example |\n",
    "|----------|-----------|-------------|---------|\n",
    "| **Assignment** | `=` `+=` `-=` `*=` `/=` `%=` `++` `--` | Assign and update values | `x = 5`, `x += 3` |\n",
    "| **Arithmetic** | `+` `-` `*` `/` `%` | Mathematical operations | `5 + 3`, `14 / 4`, `14 % 4` |\n",
    "| **Comparison** | `==` `!=` `>` `<` `>=` `<=` | Compare values, return `bool` | `5 > 3`, `x == y` |\n",
    "| **Logical** | `&&` `\\|\\|` `!` | Combine or invert `bool` expressions | `x > 0 && x < 10` |\n",
    "| **Bitwise** | `&` `\\|` `^` `~` `<<` `>>` | Bit-level operations on integers | `5 & 3`, `5 << 1` |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42ab5e7d",
   "metadata": {},
   "source": [
    "(assignment-operators)="
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97ba6e44",
   "metadata": {},
   "source": [
    "(logical-operators)=\n",
    "\n",
    "```{index} C# operators; operator types\n",
    "```\n",
    "\n",
    "## C# Operator Types\n",
    "\n",
    "Every expression in C# is built from **operands** (values or variables) and\n",
    "**operators** (symbols that define the operation). Operators are grouped by\n",
    "what they do to their operands.\n",
    "\n",
    ":::{note}\n",
    "**C# vs Other Languages**: Different language may have different operators doing the same thing.\n",
    "\n",
    "| Feature | C# | C / C++ | Python | JavaScript |\n",
    "|---------|----|---------|----|------------|\n",
    "| Logical AND / OR | `&&` `\\|\\|` | `&&` `\\|\\|` | `and` `or` | `&&` `\\|\\|` |\n",
    "| Logical NOT | `!` | `!` | `not` | `!` |\n",
    "| Integer division | `/` (auto when both `int`) | `/` (auto) | `//` | no built-in |\n",
    "| Remainder | `%` | `%` | `%` | `%` |\n",
    "| Exponentiation | `Math.Pow(x, y)` | `pow(x,y)` | `**` | `**` |\n",
    "| Increment | `++` `--` | `++` `--` | no `++`/`--` | `++` `--` |\n",
    "| String equality | `==` (value) | `==` (pointer!) | `==` | `==` (loose) / `===` |\n",
    "| Null coalescing | `??` | — | `or` idiom | `??` |\n",
    "\n",
    "In C# and JS, `&&`/`\\|\\|` use *short-circuit* evaluation: the right-hand side\n",
    "is only evaluated if needed. Python's `and`/`or` do the same but return one of\n",
    "the operands rather than a strict `bool`. C/C++ lack a dedicated boolean type\n",
    "in older standards — `0` is false, anything else is true. [^1]\n",
    ":::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3160ccf5",
   "metadata": {},
   "source": [
    "```{index} assignment operator; compound assignment\n",
    "```\n",
    "\n",
    "### Assignment Operators\n",
    "\n",
    "An **assignment operator** stores a value into a variable. The basic form is\n",
    "`=`, which should not be confused with `==` (equality test). C# also provides\n",
    "**compound assignment operators** that combine an arithmetic operation with\n",
    "assignment in one step, making code more concise:\n",
    "\n",
    "```csharp\n",
    "int x = 10;   // assign\n",
    "x += 3;       // x is now 13  (same as: x = x + 3)\n",
    "x -= 2;       // x is now 11\n",
    "x++;          // x is now 12  (post-increment)\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c118bfea",
   "metadata": {},
   "source": [
    "```{index} arithmetic operator\n",
    "```\n",
    "\n",
    "### Arithmetic Operators\n",
    "\n",
    "**Arithmetic operators** perform standard mathematical operations. An important\n",
    "subtlety in C# is **integer division**: when both operands are `int`, the `/`\n",
    "operator truncates toward zero and discards any remainder. To get a decimal\n",
    "result, at least one operand must be a `double` (or another floating-point\n",
    "type):\n",
    "\n",
    "```csharp\n",
    "10 / 3;     // → 3  (integer division, remainder discarded)\n",
    "10.0 / 3;   // → 3.333...  (double division)\n",
    "10 % 3;     // → 1  (remainder)\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d30ca8a",
   "metadata": {},
   "source": [
    "```{index} comparison operator; relational operator\n",
    "```\n",
    "\n",
    "### Comparison Operators\n",
    "\n",
    "**Comparison operators** (also called *relational operators*) compare two\n",
    "values and always return a `bool` — either `true` or `false`. They are the\n",
    "primary building blocks of conditions in `if` statements and loops:\n",
    "\n",
    "```csharp\n",
    "int a = 5, b = 3;\n",
    "a == b;   // false — equality (note: two equals signs, not one)\n",
    "a != b;   // true  — not equal\n",
    "a > b;    // true  — greater than\n",
    "```\n",
    "\n",
    "A very common bug is writing `=` (assignment) instead of `==` (equality test)\n",
    "inside a condition. C# will often catch this as a compile-time error.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efeadde9",
   "metadata": {},
   "source": [
    "```{index} logical operator; short-circuit evaluation\n",
    "```\n",
    "\n",
    "### Logical Operators\n",
    "\n",
    "**Logical operators** combine `bool` expressions to build compound conditions.\n",
    "C# evaluates them using *short-circuit* rules: for `&&`, if the left side is\n",
    "`false`, the right side is skipped; for `||`, if the left side is `true`, the\n",
    "right side is skipped. This matters when the right side has side-effects or\n",
    "could throw an exception.\n",
    "\n",
    "Precedence: `!` binds tightest, then `&&`, then `||`. Use parentheses for\n",
    "clarity when mixing them:\n",
    "\n",
    "```csharp\n",
    "bool x = true, y = false;\n",
    "x && y;   // false — both must be true\n",
    "x || y;   // true  — at least one must be true\n",
    "!x;       // false — inverts x\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3987b65",
   "metadata": {},
   "source": [
    "```{index} bitwise operator; bit manipulation\n",
    "```\n",
    "\n",
    "### Bitwise Operators\n",
    "\n",
    "**Bitwise operators** work on the individual bits of integer values. They are\n",
    "used in low-level programming, flags, masks, and performance-sensitive code.\n",
    "Each bit in the result is computed independently from the corresponding bits of\n",
    "the operands:\n",
    "\n",
    "```text\n",
    "  5 in binary  →  0101\n",
    "  3 in binary  →  0011\n",
    "  5 & 3        →  0001  (AND: 1 only where both are 1)\n",
    "  5 | 3        →  0111  (OR:  1 where either is 1)\n",
    "  5 ^ 3        →  0110  (XOR: 1 where exactly one is 1)\n",
    "```\n",
    "\n",
    "Shift operators (`<<`, `>>`) move bits left or right, which is equivalent to\n",
    "multiplying or dividing by powers of 2:\n",
    "\n",
    "```csharp\n",
    "5 << 1;   // → 10  (5 × 2)\n",
    "5 >> 1;   // → 2   (5 ÷ 2, integer)\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c31f907",
   "metadata": {},
   "source": [
    "## Operation Examples\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd10f31d",
   "metadata": {},
   "source": [
    "### Assignment\n",
    "\n",
    "The `=` operator assigns a value. Compound operators (`+=`, `-=`, etc.) combine an operation with assignment. `++` and `--` increment or decrement by 1:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7cf6e304",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "ename": "Error",
     "evalue": "(2,4): error CS1002: ; expected",
     "output_type": "error",
     "traceback": [
      "(2,4): error CS1002: ; expected"
     ]
    }
   ],
   "source": [
    "x = 1;\n",
    "x++       // post-increment operator; x++ increments the value of variable x after it's evaluated in an expression\n",
    "x\n",
    "x = 2;\n",
    "++x       // pre-increment operator; ++x increments the value of variable x before it's evaluated in an expression"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73b1ecdf",
   "metadata": {},
   "source": [
    "### Arithmetic\n",
    "\n",
    "Basic arithmetic operations on numeric types:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "031dc42c",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "ename": "Error",
     "evalue": "(2,6): error CS1002: ; expected\n(3,6): error CS1002: ; expected\n(4,6): error CS1002: ; expected\n(5,6): error CS1002: ; expected",
     "output_type": "error",
     "traceback": [
      "(2,6): error CS1002: ; expected\n",
      "(3,6): error CS1002: ; expected\n",
      "(4,6): error CS1002: ; expected\n",
      "(5,6): error CS1002: ; expected"
     ]
    }
   ],
   "source": [
    "int a = 10, b = 3;\n",
    "a + b   // 13 — addition\n",
    "a - b   // 7  — subtraction\n",
    "a * b   // 30 — multiplication\n",
    "a / b   // 3  — integer division (truncates toward zero)\n",
    "a % b   // 1  — remainder"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70bf620a",
   "metadata": {},
   "source": [
    "```{index} operator; division and remainder\n",
    "```\n",
    "\n",
    "(division-and-remainders)=\n",
    "\n",
    "### Division\n",
    "\n",
    "Division can be a little tricky in C#. For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f35498fb",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "ename": "Error",
     "evalue": "(1,1): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement\n(2,1): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement",
     "output_type": "error",
     "traceback": [
      "(1,1): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement\n",
      "(2,1): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement"
     ]
    }
   ],
   "source": [
    "5.0/2.0;\n",
    "14.0/4.0;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4d79135",
   "metadata": {},
   "source": [
    "But C# will implicitly turn the following expression to an `int` type:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8f8bd436",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"dni-plaintext\"><pre>3</pre></div><style>\r\n",
       ".dni-code-hint {\r\n",
       "    font-style: italic;\r\n",
       "    overflow: hidden;\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview {\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview td {\r\n",
       "    vertical-align: top;\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "details.dni-treeview {\r\n",
       "    padding-left: 1em;\r\n",
       "}\r\n",
       "table td {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "table tr { \r\n",
       "    vertical-align: top; \r\n",
       "    margin: 0em 0px;\r\n",
       "}\r\n",
       "table tr td pre \r\n",
       "{ \r\n",
       "    vertical-align: top !important; \r\n",
       "    margin: 0em 0px !important;\r\n",
       "} \r\n",
       "table th {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "</style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "14/4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42aa38d6",
   "metadata": {},
   "source": [
    "Adding a decimal point would inform C# that you are using `double` instead of `int`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ebb5ee3f",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "ename": "Error",
     "evalue": "(1,9): error CS1002: ; expected\n(2,9): error CS1002: ; expected",
     "output_type": "error",
     "traceback": [
      "(1,9): error CS1002: ; expected\n",
      "(2,9): error CS1002: ; expected"
     ]
    }
   ],
   "source": [
    "14.0/4.0\n",
    "14.0 / 4      // one of the operands is double type\n",
    "6.0/3.0       // when the remainder is 0, the quotient is an int."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93584448",
   "metadata": {},
   "source": [
    "Note that C# stores values with only a limited precision, so the results of\n",
    "mathematical operations are only approximate in general. The following\n",
    "is an example that shows the `double` type has a precision of ~15-17 digits:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3cbab452",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"dni-plaintext\"><pre>0.3333333333333333</pre></div><style>\r\n",
       ".dni-code-hint {\r\n",
       "    font-style: italic;\r\n",
       "    overflow: hidden;\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview {\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview td {\r\n",
       "    vertical-align: top;\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "details.dni-treeview {\r\n",
       "    padding-left: 1em;\r\n",
       "}\r\n",
       "table td {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "table tr { \r\n",
       "    vertical-align: top; \r\n",
       "    margin: 0em 0px;\r\n",
       "}\r\n",
       "table tr td pre \r\n",
       "{ \r\n",
       "    vertical-align: top !important; \r\n",
       "    margin: 0em 0px !important;\r\n",
       "} \r\n",
       "table th {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "</style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "1.0/3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e21456a6",
   "metadata": {},
   "source": [
    "### Remainders\n",
    "\n",
    "Remainder is used to check if a number is divisible by another number.\n",
    "The remainder operator `%` computes the remainder after dividing its\n",
    "left-hand operand by its right-hand operand.\n",
    "\n",
    "Try in the `csharprepl`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1c44ca22",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "ename": "Error",
     "evalue": "(1,7): error CS1002: ; expected\n(2,7): error CS1002: ; expected",
     "output_type": "error",
     "traceback": [
      "(1,7): error CS1002: ; expected\n",
      "(2,7): error CS1002: ; expected"
     ]
    }
   ],
   "source": [
    "14 % 7\n",
    "14 % 4\n",
    "int x = 0;\n",
    "x = 7 % 5;  // x now contains 2\n",
    "x = 9 % 5;  // x now contains 4\n",
    "x = 5 % 5;  // x now contains 0\n",
    "x = 4 % 5;  // x now contains 4\n",
    "x = -4 % 5; // x now contains -4\n",
    "x = 4 % -5; // x now contains 4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "054baee2",
   "metadata": {},
   "source": [
    "The precedence of `%` is the same as `/` and `*`, and hence\n",
    "higher than addition and subtraction, `+` and `-`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc268d8f",
   "metadata": {},
   "source": [
    "### Comparison\n",
    "\n",
    "Comparison operators return a `bool` (`true` or `false`):\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6e7238c5",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "ename": "Error",
     "evalue": "(2,7): error CS1002: ; expected\n(3,6): error CS1003: Syntax error, ',' expected\n(4,3): error CS1003: Syntax error, ',' expected\n(4,6): error CS1003: Syntax error, ',' expected\n(4,7): error CS1003: Syntax error, '>' expected",
     "output_type": "error",
     "traceback": [
      "(2,7): error CS1002: ; expected\n",
      "(3,6): error CS1003: Syntax error, ',' expected\n",
      "(4,3): error CS1003: Syntax error, ',' expected\n",
      "(4,6): error CS1003: Syntax error, ',' expected\n",
      "(4,7): error CS1003: Syntax error, '>' expected"
     ]
    }
   ],
   "source": [
    "int x = 1, y = 2;\n",
    "x == y\n",
    "x < y\n",
    "x <= y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31335145",
   "metadata": {},
   "source": [
    "### Logical\n",
    "\n",
    "Logical operators combine or invert `bool` expressions:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "931b17c6",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "ename": "Error",
     "evalue": "(2,16): error CS1002: ; expected",
     "output_type": "error",
     "traceback": [
      "(2,16): error CS1002: ; expected"
     ]
    }
   ],
   "source": [
    "int x = 2;\n",
    "x < 5 && x < 10\n",
    "x < 5 || x < 10\n",
    "!(x < 5 && x < 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8148bfdb",
   "metadata": {},
   "source": [
    "### Bitwise\n",
    "\n",
    "Bitwise operators work directly on the binary representation of integers:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cfd0fb43",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "ename": "Error",
     "evalue": "(2,6): error CS1002: ; expected\n(3,6): error CS1002: ; expected\n(4,6): error CS1002: ; expected\n(5,3): error CS1002: ; expected\n(6,7): error CS1002: ; expected",
     "output_type": "error",
     "traceback": [
      "(2,6): error CS1002: ; expected\n",
      "(3,6): error CS1002: ; expected\n",
      "(4,6): error CS1002: ; expected\n",
      "(5,3): error CS1002: ; expected\n",
      "(6,7): error CS1002: ; expected"
     ]
    }
   ],
   "source": [
    "int x = 5, y = 3;  // binary: x = 0101, y = 0011\n",
    "x & y    // Bitwise AND:  0101 & 0011 = 0001 → 1\n",
    "x | y    // Bitwise OR:   0101 | 0011 = 0111 → 7\n",
    "x ^ y    // Bitwise XOR:  0101 ^ 0011 = 0110 → 6\n",
    "~x       // Bitwise NOT:  ~0101 = ...11111010 → -6\n",
    "x << 1   // Left shift:   0101 << 1 = 1010 → 10\n",
    "x >> 1   // Right shift:  0101 >> 1 = 0010 → 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dd511fd",
   "metadata": {},
   "source": [
    "```{index} precedence; operator\n",
    "```\n",
    "\n",
    "(precedence)=\n",
    "\n",
    "## Operator Precedence\n",
    "\n",
    "Earlier lines have higher precedence.\n",
    "Only operators used in this book are included:\n",
    "\n",
    "```none\n",
    "obj.field  f(x)  a[i]  n++  n--  new\n",
    "+  -  ! (Type)x  (Unary operators)\n",
    "* / %\n",
    "+ - (binary)\n",
    "< > <= >=\n",
    "== !=\n",
    "&&\n",
    "||\n",
    "=  *=  /=  %=  +=  -=\n",
    "```\n",
    "\n",
    "All symbols are listed at the beginning of the index.\n",
    "\n",
    "Parentheses for grouping are encouraged with less common combinations, even if not strictly necessary.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e842b09e",
   "metadata": {},
   "source": [
    "## C# Operator Reference\n",
    "\n",
    "A more detailed overview of all the operators in C#:\n",
    "\n",
    "| No. | Type | Operator | Meaning | Example | Result |\n",
    "|-----|------|----------|---------|---------|--------|\n",
    "| 1 | **Assignment** | `=` | Assign | `x = 5` | `x` is `5` |\n",
    "| | | `+=` | Add and assign | `x += 3` | `x = x + 3` |\n",
    "| | | `-=` | Subtract and assign | `x -= 3` | `x = x - 3` |\n",
    "| | | `*=` | Multiply and assign | `x *= 3` | `x = x * 3` |\n",
    "| | | `/=` | Divide and assign | `x /= 3` | `x = x / 3` |\n",
    "| | | `%=` | Remainder and assign | `x %= 3` | `x = x % 3` |\n",
    "| | | `++` | Increment by 1 | `x++` | `x = x + 1` |\n",
    "| | | `--` | Decrement by 1 | `x--` | `x = x - 1` |\n",
    "| 2 | **Arithmetic** | `+` | Addition | `5 + 3` | `8` |\n",
    "| | | `-` | Subtraction | `5 - 3` | `2` |\n",
    "| | | `*` | Multiplication | `5 * 3` | `15` |\n",
    "| | | `/` | Division | `5.0 / 2.0` | `2.5` |\n",
    "| | | `%` | Remainder | `14 % 4` | `2` |\n",
    "| 3 | **Comparison** | `==` | Equal to | `x == y` | `true` or `false` |\n",
    "| | | `!=` | Not equal to | `x != y` | `true` or `false` |\n",
    "| | | `>` | Greater than | `5 > 3` | `true` |\n",
    "| | | `<` | Less than | `5 < 3` | `false` |\n",
    "| | | `>=` | Greater than or equal to | `5 >= 5` | `true` |\n",
    "| | | `<=` | Less than or equal to | `5 <= 3` | `false` |\n",
    "| 4 | **Logical** | `&&` | AND — both must be `true` | `x > 0 && x < 10` | `true` or `false` |\n",
    "| | | `\\|\\|` | OR — at least one `true` | `x < 0 \\|\\| x > 10` | `true` or `false` |\n",
    "| | | `!` | NOT — inverts value | `!(x > 10)` | `true` or `false` |\n",
    "| 5 | **Bitwise** | `&` | Bitwise AND | `5 & 3` | `1` |\n",
    "| | | `\\|` | Bitwise OR | `5 \\| 3` | `7` |\n",
    "| | | `^` | Bitwise XOR | `5 ^ 3` | `6` |\n",
    "| | | `~` | Bitwise NOT (complement) | `~5` | `-6` |\n",
    "| | | `<<` | Left shift | `5 << 1` | `10` |\n",
    "| | | `>>` | Right shift | `5 >> 1` | `2` |\n",
    "| 6 | **Ternary** | `? :` | Conditional — if/else shorthand | `x > 0 ? \"pos\" : \"neg\"` | `\"pos\"` or `\"neg\"` |\n",
    "| 7 | **Null Coalescing** | `??` | Return left if not null, else right | `name ?? \"Unknown\"` | `\"Unknown\"` if `name` is `null` |\n",
    "| | | `??=` | Assign only if null | `name ??= \"Unknown\"` | `name` set to `\"Unknown\"` if `null` |\n",
    "| 8 | **Type Testing** | `is` | Check if object is a type | `x is int` | `true` or `false` |\n",
    "| | | `as` | Cast to type, returns null if fails | `obj as string` | `string` or `null` |\n",
    "| | | `typeof` | Get type information | `typeof(int)` | `System.Int32` |\n",
    "| 9 | **Member Access** | `.` | Access member of object | `obj.Name` | value of `Name` |\n",
    "| | | `?.` | Access member, null if object is null | `obj?.Name` | value or `null` |\n",
    "| | | `[]` | Access element by index | `arr[0]` | first element |\n",
    "\n",
    "\\* For exponentiation, C# uses `Math.Pow()` where Python uses `**`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1feac3c",
   "metadata": {},
   "source": [
    "```{rubric} Footnotes\n",
    "```\n",
    "\n",
    "[^1]: For a complete discussion on C# operators, see the\n",
    "[C# documentation on operators](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a9e92c8",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "polyglot-notebook",
   "version": ""
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
