From 157b18e9be867365babdd7eaaf28df28710e4670 Mon Sep 17 00:00:00 2001 From: Magnus-Mage Date: Thu, 15 Jan 2026 07:56:41 +0000 Subject: [PATCH 01/16] Files changed: * wiki/wiki/cpp-tutorial/operators.md: Added a doc for operators in C++ Sign off: Magnus-Mage <> --- wiki/cpp-tutorial/operators.md | 478 ++++++++++++++++++++++++++++++++- 1 file changed, 472 insertions(+), 6 deletions(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index bf55e73..3aca8bb 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -1,15 +1,481 @@ ---- -wip: true ---- - # Operators -## What is an Operator? +In any programming language, the core standard provides simple ways to do various operations on numbers, variables, pointers, even functions with operator overloading. In C++, we are also provided with various operators that we can use to do different tasks. Usually they have the same symbols and methodology as elementary mathematics. We will discuss what are operators and how are they separated into groups by the C++ standard below. + +## What Are Operators? + +As discussed above, operators are symbols used to perform operations on variables or values. For example: + +```c++ +int value = 5 + 10; // value = 15 +``` + +We can also do operations between variables or even variables and values like: + +```c++ +int a = 5; +int b = 10; +int value = a + b; // value = 15 +// OR +int result = 10 + a; // result = 15 +``` + +Operators in C++ can be classified into 6 groups: + +1. [Arithmetic Operators](#Arithmetic Operators) +2. [Assignment Operators](#Assignment Operators) +3. [Relational Operators](#Relational Operators) +4. [Logical Operators](#Logical Operators) +5. [Bitwise Operators](#Bitwise Operators) +6. [Other Operators](#Other Operators) + +## Arithmetic Operators + +Arithmetic operators are used to perform mathematical calculations. These are the most commonly used operators and work similarly to basic mathematics. + +|Operator|Description|Example|Result| +|---|---|---|---| +|`+`|Addition|`5 + 3`|`8`| +|`-`|Subtraction|`5 - 3`|`2`| +|`*`|Multiplication|`5 * 3`|`15`| +|`/`|Division|`10 / 2`|`5`| +|`%`|Modulus/Reminder|`10 % 3`|`1`| +|`++`|Increment|`int a = 5; a++`|`6`| +|`--`|Decrement|`int a = 5; a--`|`4`| + +### Basic Examples for Arithmetic Operators: + +```c++ +int a = 10; +int b = 3; + +int sum = a + b; // 13 +int difference = a - b; // 7 +int product = a * b; // 30 +int quotient = a / b; // 3 (integer division) +int remainder = a % b; // 1 +``` + +### Division Behavior + +Division in C++ behaves differently based on the types of the values or variables involved. Some examples are given below: + +```c++ +int x = 7 / 2; // 3 (integer division, decimal part truncated) +double y = 7.0 / 2; // 3.5 (floating-point division) +double z = 7 / 2.0; // 3.5 (at least one operand is floating-point) +``` + +### Increment and Decrement + +The increment (`++`) and decrement (`--`) operators have two forms: prefix and postfix. + +```c++ +int a = 5; +int b = ++a; // Prefix: increment a first, then assign. a = 6, b = 6 +int c = a++; // Postfix: assign a first, then increment. a = 7, c = 6 + +int x = 10; +int y = --x; // Prefix: decrement x first, then assign. x = 9, y = 9 +int z = x--; // Postfix: assign x first, then decrement. x = 8, z = 9 +``` + +## Assignment Operators + +Assignment operators are used to assign values to variables. The basic assignment operator is `=`, but C++ provides compound assignment operators that combine arithmetic operations with assignment. The assignment is done from left to right, the value of left is assigned the value on right, in some cases after an operation. + +| Operator | Description | Example | Equivalent To | +| -------- | ---------------------- | --------- | ------------- | +| `=` | Simple assignment | `a = 5` | `a = 5` | +| `+=` | Add and assign | `a += 3` | `a = a + 3` | +| `-=` | Subtract and assign | `a -= 3` | `a = a - 3` | +| `*=` | Multiply and assign | `a *= 3` | `a = a * 3` | +| `/=` | Divide and assign | `a /= 3` | `a = a / 3` | +| `%=` | Modulus and assign | `a %= 3` | `a = a % 3` | +| `&=` | Bitwise AND and assign | `a &= 3` | `a = a & 3` | +| `\|=` | Bitwise OR and assign | `a \|= 3` | `a = a \| 3` | +| `^=` | Bitwise XOR and assign | `a ^= 3` | `a = a ^ 3` | +| `<<=` | Left shift and assign | `a <<= 2` | `a = a << 2` | +| `>>=` | Right shift and assign | `a >>= 2` | `a = a >> 2` | + +### Examples + +```c++ +int value = 10; +value += 5; // value = 15 +value -= 3; // value = 12 +value *= 2; // value = 24 +value /= 4; // value = 6 +value %= 4; // value = 2 +``` + +Compound assignment operators are more concise and can sometimes be more efficient than their long-form equivalents. + +## Relational Operators + +Relational operators (also called comparison operators) are used to compare two values. They return a boolean result: `true` or `false`. + +|Operator|Description|Example|Result| +|---|---|---|---| +|`==`|Equal to|`5 == 5`|`true`| +|`!=`|Not equal to|`5 != 3`|`true`| +|`>`|Greater than|`5 > 3`|`true`| +|`<`|Less than|`5 < 3`|`false`| +|`>=`|Greater than or equal to|`5 >= 5`|`true`| +|`<=`|Less than or equal to|`5 <= 3`|`false`| + +### Examples + +```c++ +int a = 10; +int b = 20; -## Mathematic Operators +bool is_equal = (a == b); // false +bool is_not_equal = (a != b); // true +bool is_greater = (a > b); // false +bool is_less = (a < b); // true +bool is_greater_equal = (a >= 10); // true +bool is_less_equal = (b <= 20); // true +``` + +These operators are commonly used in conditional statements: + +```c++ +int age = 18; + +if (age >= 18) { + std::cout << "You are an adult.\n"; +} +``` + +> [!WARNING] +> +> Be careful not to confuse `=` (assignment) with `==` (comparison). Using `=` in a conditional statement is a common mistake that can lead to unexpected behavior. ## Logical Operators +Logical operators are used to combine multiple boolean expressions or to invert boolean values. They are essential for creating complex conditional statements. + +|Operator|Description|Example|Result| +|---|---|---|---| +|`&&`|Logical AND|`true && false`|`false`| +|`\|`|Logical OR|`true \| false`|`true`| +|`!`|Logical NOT|`!true`|`false`| + +### Logical AND (`&&`) + +Returns `true` only if both operands are `true`. + +```c++ +int age = 25; +bool has_license = true; + +if (age >= 18 && has_license) { + std::cout << "You can drive.\n"; +} +``` + +### Logical OR (`||`) + +Returns `true` if at least one operand is `true`. + +```c++ +bool is_weekend = false; +bool is_holiday = true; + +if (is_weekend || is_holiday) { + std::cout << "Time to relax!\n"; +} +``` + +### Logical NOT (`!`) + +Inverts the boolean value. + +```c++ +bool is_raining = false; + +if (!is_raining) { + std::cout << "It's a nice day!\n"; +} +``` + +### Short-Circuit Evaluation + +C++ uses short-circuit evaluation for logical operators: + +- For `&&`, if the left operand is `false`, the right operand is not evaluated +- For `||`, if the left operand is `true`, the right operand is not evaluated + +```c++ +int x = 5; +// The second condition is never checked because the first is false +if (x > 10 && ++x > 0) { + // This won't execute +} +// x is still 5, not 6 +``` + +This behavior can be useful for avoiding errors: + +```c++ +int* ptr = nullptr; +// Check if ptr is valid before dereferencing +if (ptr != nullptr && *ptr > 0) { + // Safe to use *ptr here +} +``` + ## Bitwise Operators +Bitwise operators perform operations on individual bits of integer types. They are useful for low-level programming, working with binary data, flags, and performance-critical code. + +|Operator|Description|Example|Result (binary)| +|---|---|---|---| +|`&`|Bitwise AND|`5 & 3`|`1` (`0101 & 0011 = 0001`)| +|`\|`|Bitwise OR|`5 \| 3`|`7` (`0101 \| 0011 = 0111`)| +|`^`|Bitwise XOR|`5 ^ 3`|`6` (`0101 ^ 0011 = 0110`)| +|`~`|Bitwise NOT|`~5`|`-6` (inverts all bits)| +|`<<`|Left shift|`5 << 1`|`10` (`0101 -> 1010`)| +|`>>`|Right shift|`5 >> 1`|`2` (`0101 -> 0010`)| + +### Bitwise AND (`&`) + +Sets each bit to 1 if both corresponding bits are 1. Otherwise sets the bit to 0. + +```c++ +unsigned int a = 12; // 1100 in binary +unsigned int b = 10; // 1010 in binary +unsigned int result = a & b; // 1000 in binary = 8 +``` + +Common use: checking if specific bits are set (masking). + +```c++ +const unsigned int READ_PERMISSION = 0b100; +unsigned int permissions = 0b110; + +if (permissions & READ_PERMISSION) { + std::cout << "Has read permission\n"; +} +``` + +### Bitwise OR (`|`) + +Sets each bit to 1 if at least one corresponding bit is 1. + +```c++ +unsigned int a = 12; // 1100 in binary +unsigned int b = 10; // 1010 in binary +unsigned int result = a | b; // 1110 in binary = 14 +``` + +Common use: setting specific bits (flags). + +```c++ +const unsigned int READ = 0b100; +const unsigned int WRITE = 0b010; +unsigned int permissions = READ | WRITE; // 0b110 +``` + +### Bitwise XOR (`^`) + +Sets each bit to 1 if the corresponding bits are different. If they are the same, set them to 0. + +```c++ +unsigned int a = 12; // 1100 in binary +unsigned int b = 10; // 1010 in binary +unsigned int result = a ^ b; // 0110 in binary = 6 +``` + +Common use: toggling bits or simple encryption. + +```c++ +int value = 42; +int key = 123; +int encrypted = value ^ key; +int decrypted = encrypted ^ key; // Back to 42 +``` + +### Bitwise NOT (`~`) + +Inverts all bits (1 becomes 0, 0 becomes 1). + +```c++ +unsigned int a = 12; // 00001100 in binary (assuming 8 bits) +unsigned int result = ~a; // 11110011 in binary +``` + +### Left Shift (`<<`) + +Shifts bits to the left, filling with zeros. Each shift left multiplies the value by 2. + +```c++ +unsigned int a = 5; // 0101 in binary +unsigned int result = a << 2; // 10100 in binary = 20 +``` + +### Right Shift (`>>`) + +Shifts bits to the right. Each shift right divides the value by 2 (integer division). + +```c++ +unsigned int a = 20; // 10100 in binary +unsigned int result = a >> 2; // 00101 in binary = 5 +``` + +> [!NOTE] +> +> For signed integers, right shift behavior is implementation-defined. Some systems perform arithmetic shift (sign extension), while others perform logical shift (zero fill). + +> [!NOTE] **For Advanced Programmers** +> +> Be aware of type promotion when using bitwise operators. Small integer types like `char` and `short` are promoted to `int` before bitwise operations, which can lead to unexpected results with the NOT operator. Also, shifting by an amount greater than or equal to the bit width of the type results in undefined behavior. + ## Other Operators + +C++ provides several other operators that don't fit into the previous groups but are essential for various programming tasks. Do keep in mind this operators are useful as you learn more about C++ so feel free to use them whenever you get the chance. + +### Ternary Operator (`? :`) + +The ternary operator is a shorthand for `if-else` statements. It takes three operands. + +**Syntax:** `condition ? value_if_true : value_if_false` + +```c++ +int a = 10; +int b = 20; +int max = (a > b) ? a : b; // max = 20 + +// This is Equivalent to: +int max; +if (a > b) { + max = a; +} else { + max = b; +} +``` + +### Comma Operator (`,`) + +The comma operator evaluates multiple expressions from left to right and returns the value of the rightmost expression. + +```c++ +int a = (5, 10, 15); // a = 15 + +int x = 1, y = 2; +int result = (++x, ++y); // x = 2, y = 3, result = 3 +``` + +It's most commonly seen in `for` loops: + +```c++ +for (int i = 0, j = 10; i < j; ++i, --j) { + // Multiple operations in the increment section +} +``` + +### Sizeof Operator + +The `sizeof` operator returns the size (in bytes) of a type or variable. + +```c++ +int size_of_int = sizeof(int); // Usually 4 +int size_of_double = sizeof(double); // Usually 8 + +int array[10]; +int array_size = sizeof(array); // 40 (assuming int is 4 bytes) +int array_length = sizeof(array) / sizeof(array[0]); // 10 +``` + +### Member Access Operators + +These operators are used to access members of structures, classes, and pointers. + +```c++ +struct Point { + int x; + int y; +}; + +Point p; +p.x = 10; // Dot operator for direct access +p.y = 20; + +Point* ptr = &p; +ptr->x = 30; // Arrow operator for pointer access +ptr->y = 40; + +// ptr->x is equivalent to (*ptr).x +``` + +### Address-Of and Dereference Operators + +The address-of operator (`&`) returns the memory address of a variable, while the dereference operator (`*`) accesses the value at a given address. + +```c++ +int value = 42; +int* ptr = &value; // ptr holds the address of value +int result = *ptr; // result = 42 (dereference ptr) + +*ptr = 100; // Changes value to 100 +``` +> [!NOTE] +> +> `&` cannot be used with a null value, meanwhile a pointer can be null. You would learn more on pointers later in the wiki. +### Scope Resolution Operator (`::`) + +The scope resolution operator is used to access global variables, static members, and namespace members. + +```c++ +int value = 10; // Global variable + +int main() { + int value = 20; // Local variable + std::cout << value << '\n'; // 20 (local) + std::cout << ::value << '\n'; // 10 (global) +} + +// Accessing namespace members +std::cout << "Hello\n"; + +// Accessing static class members +class MyClass { +public: + static int count; +}; +int x = MyClass::count; +``` + +### Cast Operators + +C++ provides several cast operators for type conversion (C++11 and later also includes more sophisticated casting mechanisms). + +```c++ +double pi = 3.14159; +int truncated = (int)pi; // C-style cast +int truncated2 = static_cast(pi); // C++ style cast (preferred) +``` + +> [!NOTE] **For Advanced Programmers** +> +> C++ provides four named cast operators: `static_cast`, `dynamic_cast`, `const_cast`, and `reinterpret_cast`. These provide more type safety and clarity than C-style casts and should be preferred in modern C++. Additionally, operator precedence and associativity become important when combining multiple operators in complex expressions. + +## Experimenting with Operators + +As you continue learning C++, you'll discover that operators can be used in creative and powerful ways. Some of this areas are operator overloading, combining operators, bitwise tricks, etc. + +The best way to master operators is through practice. Try: + +- Writing programs that use different combinations of operators +- Experimenting with operator precedence to understand evaluation order +- Implementing algorithms that leverage bitwise operations +- Creating your own classes with overloaded operators + +Remember that readable code is often more important than clever tricks. Use operators in ways that make your intent clear to other programmers (or yourself in the future). + +> [!NOTE] **For Advanced Programmers** +> +> Consider exploring topics like expression templates, SFINAE with operators, the spaceship operator (`<=>`) introduced in C++20, and how compilers optimize operator usage. The `constexpr` keyword can also be applied to operator overloads for compile-time evaluation. Understanding move semantics and perfect forwarding becomes crucial when overloading assignment operators and implementing efficient operator overloads. + + From e7947c14a51d443fb6857aa72eadd719da8f7a78 Mon Sep 17 00:00:00 2001 From: Magnus-Mage Date: Sun, 8 Feb 2026 17:10:54 +0530 Subject: [PATCH 02/16] Add: Operators doc --- wiki/cpp-tutorial/operators.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index 3aca8bb..3c756f9 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -22,12 +22,12 @@ int result = 10 + a; // result = 15 Operators in C++ can be classified into 6 groups: -1. [Arithmetic Operators](#Arithmetic Operators) -2. [Assignment Operators](#Assignment Operators) -3. [Relational Operators](#Relational Operators) -4. [Logical Operators](#Logical Operators) -5. [Bitwise Operators](#Bitwise Operators) -6. [Other Operators](#Other Operators) +1. Arithmetic Operators +2. Assignment Operators +3. Relational Operators +4. Logical Operators +5. Bitwise Operators +6. Other Operators ## Arithmetic Operators @@ -328,7 +328,7 @@ unsigned int result = a >> 2; // 00101 in binary = 5 > > For signed integers, right shift behavior is implementation-defined. Some systems perform arithmetic shift (sign extension), while others perform logical shift (zero fill). -> [!NOTE] **For Advanced Programmers** +> [!NOTE] For Advanced Programmers > > Be aware of type promotion when using bitwise operators. Small integer types like `char` and `short` are promoted to `int` before bitwise operations, which can lead to unexpected results with the NOT operator. Also, shifting by an amount greater than or equal to the bit width of the type results in undefined behavior. @@ -457,7 +457,7 @@ int truncated = (int)pi; // C-style cast int truncated2 = static_cast(pi); // C++ style cast (preferred) ``` -> [!NOTE] **For Advanced Programmers** +> [!NOTE] For Advanced Programmers > > C++ provides four named cast operators: `static_cast`, `dynamic_cast`, `const_cast`, and `reinterpret_cast`. These provide more type safety and clarity than C-style casts and should be preferred in modern C++. Additionally, operator precedence and associativity become important when combining multiple operators in complex expressions. @@ -474,7 +474,7 @@ The best way to master operators is through practice. Try: Remember that readable code is often more important than clever tricks. Use operators in ways that make your intent clear to other programmers (or yourself in the future). -> [!NOTE] **For Advanced Programmers** +> [!NOTE] For Advanced Programmers > > Consider exploring topics like expression templates, SFINAE with operators, the spaceship operator (`<=>`) introduced in C++20, and how compilers optimize operator usage. The `constexpr` keyword can also be applied to operator overloads for compile-time evaluation. Understanding move semantics and perfect forwarding becomes crucial when overloading assignment operators and implementing efficient operator overloads. From 96f4e34ceadfae154a053e01bb394a711e7e8321 Mon Sep 17 00:00:00 2001 From: Magnus-Mage Date: Sun, 2 Aug 2026 22:55:31 +0530 Subject: [PATCH 03/16] Changes to operator article --- wiki/cpp-tutorial/operators.md | 46 +++++++++++++--------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index 3c756f9..4dae5d7 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -1,10 +1,10 @@ # Operators -In any programming language, the core standard provides simple ways to do various operations on numbers, variables, pointers, even functions with operator overloading. In C++, we are also provided with various operators that we can use to do different tasks. Usually they have the same symbols and methodology as elementary mathematics. We will discuss what are operators and how are they separated into groups by the C++ standard below. +Operators are symbols that perform operations on numbers, variables, pointers, and even functions with operator overloading. C++ operators mostly share the same symbols and behavior you already know from elementary mathematics. Below is a breakdown of the operator groups defined by the C++ standard. ## What Are Operators? -As discussed above, operators are symbols used to perform operations on variables or values. For example: +Operators are symbols used to perform operations on variables or values. For example: ```c++ int value = 5 + 10; // value = 15 @@ -82,7 +82,7 @@ int z = x--; // Postfix: assign x first, then decrement. x = 8, z = 9 ## Assignment Operators -Assignment operators are used to assign values to variables. The basic assignment operator is `=`, but C++ provides compound assignment operators that combine arithmetic operations with assignment. The assignment is done from left to right, the value of left is assigned the value on right, in some cases after an operation. +Assignment operators are used to assign values to variables. The basic assignment operator is `=`, but C++ provides compound assignment operators that combine arithmetic operations with assignment. Assignment is evaluated right to left: the value on the right-hand side is assigned to the left-hand side, in some cases after an operation. | Operator | Description | Example | Equivalent To | | -------- | ---------------------- | --------- | ------------- | @@ -159,7 +159,7 @@ Logical operators are used to combine multiple boolean expressions or to invert |Operator|Description|Example|Result| |---|---|---|---| |`&&`|Logical AND|`true && false`|`false`| -|`\|`|Logical OR|`true \| false`|`true`| +|`\|\|`|Logical OR|`true \| false`|`true`| |`!`|Logical NOT|`!true`|`false`| ### Logical AND (`&&`) @@ -326,19 +326,15 @@ unsigned int result = a >> 2; // 00101 in binary = 5 > [!NOTE] > -> For signed integers, right shift behavior is implementation-defined. Some systems perform arithmetic shift (sign extension), while others perform logical shift (zero fill). - -> [!NOTE] For Advanced Programmers -> -> Be aware of type promotion when using bitwise operators. Small integer types like `char` and `short` are promoted to `int` before bitwise operations, which can lead to unexpected results with the NOT operator. Also, shifting by an amount greater than or equal to the bit width of the type results in undefined behavior. +> For signed integers, right shift is defined by the standard to perform an arithmetic shift (the sign bit is preserved). This differs from unsigned integers, where right shift is a logical shift that fills with zeros. ## Other Operators -C++ provides several other operators that don't fit into the previous groups but are essential for various programming tasks. Do keep in mind this operators are useful as you learn more about C++ so feel free to use them whenever you get the chance. +C++ provides several other operators that don't fit into the previous groups but are essential for various programming tasks. These become more useful as you write more C++, so feel free to explore them as you go. -### Ternary Operator (`? :`) +### Conditional Operator (`? :`) -The ternary operator is a shorthand for `if-else` statements. It takes three operands. +The conditional operator (informally called the "ternary operator") is a shorthand for `if-else` statements. It takes three operands. **Syntax:** `condition ? value_if_true : value_if_false` @@ -347,7 +343,7 @@ int a = 10; int b = 20; int max = (a > b) ? a : b; // max = 20 -// This is Equivalent to: +// This is equivalent to: int max; if (a > b) { max = a; @@ -363,14 +359,17 @@ The comma operator evaluates multiple expressions from left to right and returns ```c++ int a = (5, 10, 15); // a = 15 -int x = 1, y = 2; +int x = 1; +int y = 2; int result = (++x, ++y); // x = 2, y = 3, result = 3 ``` -It's most commonly seen in `for` loops: +It's most commonly seen in the increment section of `for` loops: ```c++ -for (int i = 0, j = 10; i < j; ++i, --j) { +int i = 0; +int j = 10; +for (; i < j; ++i, --j) { // Multiple operations in the increment section } ``` @@ -382,10 +381,6 @@ The `sizeof` operator returns the size (in bytes) of a type or variable. ```c++ int size_of_int = sizeof(int); // Usually 4 int size_of_double = sizeof(double); // Usually 8 - -int array[10]; -int array_size = sizeof(array); // 40 (assuming int is 4 bytes) -int array_length = sizeof(array) / sizeof(array[0]); // 10 ``` ### Member Access Operators @@ -423,6 +418,7 @@ int result = *ptr; // result = 42 (dereference ptr) > [!NOTE] > > `&` cannot be used with a null value, meanwhile a pointer can be null. You would learn more on pointers later in the wiki. + ### Scope Resolution Operator (`::`) The scope resolution operator is used to access global variables, static members, and namespace members. @@ -463,7 +459,7 @@ int truncated2 = static_cast(pi); // C++ style cast (preferred) ## Experimenting with Operators -As you continue learning C++, you'll discover that operators can be used in creative and powerful ways. Some of this areas are operator overloading, combining operators, bitwise tricks, etc. +As you continue learning C++, you'll discover that operators can be used in creative and powerful ways. Some of these areas are operator overloading, combining operators, bitwise tricks, etc. The best way to master operators is through practice. Try: @@ -472,10 +468,4 @@ The best way to master operators is through practice. Try: - Implementing algorithms that leverage bitwise operations - Creating your own classes with overloaded operators -Remember that readable code is often more important than clever tricks. Use operators in ways that make your intent clear to other programmers (or yourself in the future). - -> [!NOTE] For Advanced Programmers -> -> Consider exploring topics like expression templates, SFINAE with operators, the spaceship operator (`<=>`) introduced in C++20, and how compilers optimize operator usage. The `constexpr` keyword can also be applied to operator overloads for compile-time evaluation. Understanding move semantics and perfect forwarding becomes crucial when overloading assignment operators and implementing efficient operator overloads. - - +We highly recommend writing code that is clear and readable to others (and your future self) and avoid clever tricks that can make code harder to read. From 97bdefe4135d77f2d2af8c8ce5a2458e80f1aa3f Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:58:16 -0400 Subject: [PATCH 04/16] Update wiki/cpp-tutorial/operators.md --- wiki/cpp-tutorial/operators.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index 4dae5d7..c15537b 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -1,3 +1,7 @@ +--- +wip: true +--- + # Operators Operators are symbols that perform operations on numbers, variables, pointers, and even functions with operator overloading. C++ operators mostly share the same symbols and behavior you already know from elementary mathematics. Below is a breakdown of the operator groups defined by the C++ standard. From 2f2a2ab642916bb4b9f25d8564e8039dcf9a1448 Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:58:24 -0400 Subject: [PATCH 05/16] Update wiki/cpp-tutorial/operators.md --- wiki/cpp-tutorial/operators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index c15537b..b215811 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -43,7 +43,7 @@ Arithmetic operators are used to perform mathematical calculations. These are th |`-`|Subtraction|`5 - 3`|`2`| |`*`|Multiplication|`5 * 3`|`15`| |`/`|Division|`10 / 2`|`5`| -|`%`|Modulus/Reminder|`10 % 3`|`1`| +|`%`|Reminder|`10 % 3`|`1`| |`++`|Increment|`int a = 5; a++`|`6`| |`--`|Decrement|`int a = 5; a--`|`4`| From 1094bf180da97861cbffc5a256f5e7bdcbc5e34e Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:58:34 -0400 Subject: [PATCH 06/16] Update wiki/cpp-tutorial/operators.md --- wiki/cpp-tutorial/operators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index b215811..0bfbdc2 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -113,7 +113,7 @@ value /= 4; // value = 6 value %= 4; // value = 2 ``` -Compound assignment operators are more concise and can sometimes be more efficient than their long-form equivalents. +Compound assignment operators are a shorter way to update a variable based on its current value. For some types with overloaded operators they may also be faster, though compiler optimizations make most equivalent. ## Relational Operators From d6fc92b87f1edf0e0b2264d9c0871b5a3e9b129e Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:58:43 -0400 Subject: [PATCH 07/16] Update wiki/cpp-tutorial/operators.md --- wiki/cpp-tutorial/operators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index 0bfbdc2..4280a78 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -163,7 +163,7 @@ Logical operators are used to combine multiple boolean expressions or to invert |Operator|Description|Example|Result| |---|---|---|---| |`&&`|Logical AND|`true && false`|`false`| -|`\|\|`|Logical OR|`true \| false`|`true`| +|`\|\|`|Logical OR|`true \|\| false`|`true`| |`!`|Logical NOT|`!true`|`false`| ### Logical AND (`&&`) From 178919817b4ec0d380bfd09afaf5bb1cb7ff3ff0 Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:58:59 -0400 Subject: [PATCH 08/16] Update wiki/cpp-tutorial/operators.md --- wiki/cpp-tutorial/operators.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index 4280a78..3f00230 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -292,13 +292,12 @@ unsigned int b = 10; // 1010 in binary unsigned int result = a ^ b; // 0110 in binary = 6 ``` -Common use: toggling bits or simple encryption. +Common use: toggling specific bits. ```c++ -int value = 42; -int key = 123; -int encrypted = value ^ key; -int decrypted = encrypted ^ key; // Back to 42 +unsigned int flags = 0b101; +flags ^= 0b001; // 0b100 (bit 0 turned off) +flags ^= 0b001; // 0b101 (bit 0 turned back on) ``` ### Bitwise NOT (`~`) From c2a7529c0ec8af43f517385edeef1d70443fe2b3 Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:59:12 -0400 Subject: [PATCH 09/16] Update wiki/cpp-tutorial/operators.md --- wiki/cpp-tutorial/operators.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index 3f00230..8f81c2a 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -305,8 +305,8 @@ flags ^= 0b001; // 0b101 (bit 0 turned back on) Inverts all bits (1 becomes 0, 0 becomes 1). ```c++ -unsigned int a = 12; // 00001100 in binary (assuming 8 bits) -unsigned int result = ~a; // 11110011 in binary +uint8_t a = 12; // 00001100 in binary +uint8_t result = ~a; // 11110011 in binary ``` ### Left Shift (`<<`) From 8535e0a204c3510290cae2d6686e51269e8d8e4a Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:59:23 -0400 Subject: [PATCH 10/16] Update wiki/cpp-tutorial/operators.md --- wiki/cpp-tutorial/operators.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index 8f81c2a..1cbbd99 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -328,8 +328,12 @@ unsigned int result = a >> 2; // 00101 in binary = 5 ``` > [!NOTE] -> -> For signed integers, right shift is defined by the standard to perform an arithmetic shift (the sign bit is preserved). This differs from unsigned integers, where right shift is a logical shift that fills with zeros. +> +> For signed integers, right shift performs an arithmetic shift, which preserves the sign bit. For unsigned integers, +> right shift fills with zeros. +> +> Shifting by a negative amount, or by an amount greater than or equal to the bit width of the type, is undefined +> behavior. ## Other Operators From 4d1e640d908c94cf9c6c06c0ea8203631976629e Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:59:42 -0400 Subject: [PATCH 11/16] Update wiki/cpp-tutorial/operators.md --- wiki/cpp-tutorial/operators.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index 1cbbd99..f44b2f2 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -349,8 +349,11 @@ The conditional operator (informally called the "ternary operator") is a shortha int a = 10; int b = 20; int max = (a > b) ? a : b; // max = 20 +``` + +This is equivalent to: -// This is equivalent to: +```c++ int max; if (a > b) { max = a; From b0a6ee427abe2850b549ad0955c16139e6cd6e2b Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:59:55 -0400 Subject: [PATCH 12/16] Update wiki/cpp-tutorial/operators.md --- wiki/cpp-tutorial/operators.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index f44b2f2..1de1f1a 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -384,6 +384,10 @@ for (; i < j; ++i, --j) { } ``` +> [!WARNING] +> +> The comma operator is a clever trick that is rarely the clearest choice and separate statements should be preferred. + ### Sizeof Operator The `sizeof` operator returns the size (in bytes) of a type or variable. From ed19d402c830d823d0ced29641628f0d6b9ecb0d Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:00:06 -0400 Subject: [PATCH 13/16] Update wiki/cpp-tutorial/operators.md --- wiki/cpp-tutorial/operators.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index 1de1f1a..9ba6d2d 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -429,9 +429,11 @@ int result = *ptr; // result = 42 (dereference ptr) *ptr = 100; // Changes value to 100 ``` + > [!NOTE] -> -> `&` cannot be used with a null value, meanwhile a pointer can be null. You would learn more on pointers later in the wiki. +> +> Only dereference a pointer that points to a valid object. Dereferencing a null pointer (`nullptr`) is undefined +> behavior and often crashes the program. Pointers are covered in more detail later in the tutorial. ### Scope Resolution Operator (`::`) From e2eda5075a0fa98a553094e673e240b8e40704fa Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:00:17 -0400 Subject: [PATCH 14/16] Update wiki/cpp-tutorial/operators.md --- wiki/cpp-tutorial/operators.md | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index 9ba6d2d..b2fd3c5 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -442,21 +442,16 @@ The scope resolution operator is used to access global variables, static members ```c++ int value = 10; // Global variable +struct Limits { + static constexpr int max_size = 100; +}; + int main() { int value = 20; // Local variable - std::cout << value << '\n'; // 20 (local) - std::cout << ::value << '\n'; // 10 (global) + std::cout << value << '\n'; // 20 (local) + std::cout << ::value << '\n'; // 10 (global) + std::cout << Limits::max_size << '\n'; // 100 (static member) } - -// Accessing namespace members -std::cout << "Hello\n"; - -// Accessing static class members -class MyClass { -public: - static int count; -}; -int x = MyClass::count; ``` ### Cast Operators From 4aba9fabd2a1ae12322408e559dc0d1147ac3896 Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:00:42 -0400 Subject: [PATCH 15/16] Update wiki/cpp-tutorial/operators.md --- wiki/cpp-tutorial/operators.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index b2fd3c5..2c3f371 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -456,17 +456,17 @@ int main() { ### Cast Operators -C++ provides several cast operators for type conversion (C++11 and later also includes more sophisticated casting mechanisms). +Cast operators convert a value from one type to another. ```c++ double pi = 3.14159; -int truncated = (int)pi; // C-style cast -int truncated2 = static_cast(pi); // C++ style cast (preferred) +int truncated1 = (int)pi; // 3, C-style cast +int truncated2 = static_cast(pi); // 3, C++-style cast (preferred) ``` -> [!NOTE] For Advanced Programmers -> -> C++ provides four named cast operators: `static_cast`, `dynamic_cast`, `const_cast`, and `reinterpret_cast`. These provide more type safety and clarity than C-style casts and should be preferred in modern C++. Additionally, operator precedence and associativity become important when combining multiple operators in complex expressions. +C++ has four named casts: `static_cast`, `dynamic_cast`, `const_cast`, and `reinterpret_cast`. `static_cast` is the one +you'll use most. You may also see C-style casts like `(int)pi` in older code. Prefer the named casts because they make +the kind of conversion explicit. ## Experimenting with Operators From df8f16db880d9d443489e8f461987dca35ef8489 Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:01:46 -0400 Subject: [PATCH 16/16] Format --- wiki/cpp-tutorial/operators.md | 106 +++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 45 deletions(-) diff --git a/wiki/cpp-tutorial/operators.md b/wiki/cpp-tutorial/operators.md index 2c3f371..1211d27 100644 --- a/wiki/cpp-tutorial/operators.md +++ b/wiki/cpp-tutorial/operators.md @@ -4,7 +4,9 @@ wip: true # Operators -Operators are symbols that perform operations on numbers, variables, pointers, and even functions with operator overloading. C++ operators mostly share the same symbols and behavior you already know from elementary mathematics. Below is a breakdown of the operator groups defined by the C++ standard. +Operators are symbols that perform operations on numbers, variables, pointers, and even functions with operator +overloading. C++ operators mostly share the same symbols and behavior you already know from elementary mathematics. +Below is a breakdown of the operator groups defined by the C++ standard. ## What Are Operators? @@ -35,17 +37,18 @@ Operators in C++ can be classified into 6 groups: ## Arithmetic Operators -Arithmetic operators are used to perform mathematical calculations. These are the most commonly used operators and work similarly to basic mathematics. +Arithmetic operators are used to perform mathematical calculations. These are the most commonly used operators and work +similarly to basic mathematics. -|Operator|Description|Example|Result| -|---|---|---|---| -|`+`|Addition|`5 + 3`|`8`| -|`-`|Subtraction|`5 - 3`|`2`| -|`*`|Multiplication|`5 * 3`|`15`| -|`/`|Division|`10 / 2`|`5`| -|`%`|Reminder|`10 % 3`|`1`| -|`++`|Increment|`int a = 5; a++`|`6`| -|`--`|Decrement|`int a = 5; a--`|`4`| +| Operator | Description | Example | Result | +| -------- | -------------- | ---------------- | ------ | +| `+` | Addition | `5 + 3` | `8` | +| `-` | Subtraction | `5 - 3` | `2` | +| `*` | Multiplication | `5 * 3` | `15` | +| `/` | Division | `10 / 2` | `5` | +| `%` | Reminder | `10 % 3` | `1` | +| `++` | Increment | `int a = 5; a++` | `6` | +| `--` | Decrement | `int a = 5; a--` | `4` | ### Basic Examples for Arithmetic Operators: @@ -62,7 +65,8 @@ int remainder = a % b; // 1 ### Division Behavior -Division in C++ behaves differently based on the types of the values or variables involved. Some examples are given below: +Division in C++ behaves differently based on the types of the values or variables involved. Some examples are given +below: ```c++ int x = 7 / 2; // 3 (integer division, decimal part truncated) @@ -86,7 +90,9 @@ int z = x--; // Postfix: assign x first, then decrement. x = 8, z = 9 ## Assignment Operators -Assignment operators are used to assign values to variables. The basic assignment operator is `=`, but C++ provides compound assignment operators that combine arithmetic operations with assignment. Assignment is evaluated right to left: the value on the right-hand side is assigned to the left-hand side, in some cases after an operation. +Assignment operators are used to assign values to variables. The basic assignment operator is `=`, but C++ provides +compound assignment operators that combine arithmetic operations with assignment. Assignment is evaluated right to left: +the value on the right-hand side is assigned to the left-hand side, in some cases after an operation. | Operator | Description | Example | Equivalent To | | -------- | ---------------------- | --------- | ------------- | @@ -113,20 +119,22 @@ value /= 4; // value = 6 value %= 4; // value = 2 ``` -Compound assignment operators are a shorter way to update a variable based on its current value. For some types with overloaded operators they may also be faster, though compiler optimizations make most equivalent. +Compound assignment operators are a shorter way to update a variable based on its current value. For some types with +overloaded operators they may also be faster, though compiler optimizations make most equivalent. ## Relational Operators -Relational operators (also called comparison operators) are used to compare two values. They return a boolean result: `true` or `false`. +Relational operators (also called comparison operators) are used to compare two values. They return a boolean result: +`true` or `false`. -|Operator|Description|Example|Result| -|---|---|---|---| -|`==`|Equal to|`5 == 5`|`true`| -|`!=`|Not equal to|`5 != 3`|`true`| -|`>`|Greater than|`5 > 3`|`true`| -|`<`|Less than|`5 < 3`|`false`| -|`>=`|Greater than or equal to|`5 >= 5`|`true`| -|`<=`|Less than or equal to|`5 <= 3`|`false`| +| Operator | Description | Example | Result | +| -------- | ------------------------ | -------- | ------- | +| `==` | Equal to | `5 == 5` | `true` | +| `!=` | Not equal to | `5 != 3` | `true` | +| `>` | Greater than | `5 > 3` | `true` | +| `<` | Less than | `5 < 3` | `false` | +| `>=` | Greater than or equal to | `5 >= 5` | `true` | +| `<=` | Less than or equal to | `5 <= 3` | `false` | ### Examples @@ -153,18 +161,20 @@ if (age >= 18) { ``` > [!WARNING] -> -> Be careful not to confuse `=` (assignment) with `==` (comparison). Using `=` in a conditional statement is a common mistake that can lead to unexpected behavior. +> +> Be careful not to confuse `=` (assignment) with `==` (comparison). Using `=` in a conditional statement is a common +> mistake that can lead to unexpected behavior. ## Logical Operators -Logical operators are used to combine multiple boolean expressions or to invert boolean values. They are essential for creating complex conditional statements. +Logical operators are used to combine multiple boolean expressions or to invert boolean values. They are essential for +creating complex conditional statements. -|Operator|Description|Example|Result| -|---|---|---|---| -|`&&`|Logical AND|`true && false`|`false`| -|`\|\|`|Logical OR|`true \|\| false`|`true`| -|`!`|Logical NOT|`!true`|`false`| +| Operator | Description | Example | Result | +| -------- | ----------- | ----------------- | ------- | +| `&&` | Logical AND | `true && false` | `false` | +| `\|\|` | Logical OR | `true \|\| false` | `true` | +| `!` | Logical NOT | `!true` | `false` | ### Logical AND (`&&`) @@ -232,16 +242,17 @@ if (ptr != nullptr && *ptr > 0) { ## Bitwise Operators -Bitwise operators perform operations on individual bits of integer types. They are useful for low-level programming, working with binary data, flags, and performance-critical code. +Bitwise operators perform operations on individual bits of integer types. They are useful for low-level programming, +working with binary data, flags, and performance-critical code. -|Operator|Description|Example|Result (binary)| -|---|---|---|---| -|`&`|Bitwise AND|`5 & 3`|`1` (`0101 & 0011 = 0001`)| -|`\|`|Bitwise OR|`5 \| 3`|`7` (`0101 \| 0011 = 0111`)| -|`^`|Bitwise XOR|`5 ^ 3`|`6` (`0101 ^ 0011 = 0110`)| -|`~`|Bitwise NOT|`~5`|`-6` (inverts all bits)| -|`<<`|Left shift|`5 << 1`|`10` (`0101 -> 1010`)| -|`>>`|Right shift|`5 >> 1`|`2` (`0101 -> 0010`)| +| Operator | Description | Example | Result (binary) | +| -------- | ----------- | -------- | --------------------------- | +| `&` | Bitwise AND | `5 & 3` | `1` (`0101 & 0011 = 0001`) | +| `\|` | Bitwise OR | `5 \| 3` | `7` (`0101 \| 0011 = 0111`) | +| `^` | Bitwise XOR | `5 ^ 3` | `6` (`0101 ^ 0011 = 0110`) | +| `~` | Bitwise NOT | `~5` | `-6` (inverts all bits) | +| `<<` | Left shift | `5 << 1` | `10` (`0101 -> 1010`) | +| `>>` | Right shift | `5 >> 1` | `2` (`0101 -> 0010`) | ### Bitwise AND (`&`) @@ -337,11 +348,13 @@ unsigned int result = a >> 2; // 00101 in binary = 5 ## Other Operators -C++ provides several other operators that don't fit into the previous groups but are essential for various programming tasks. These become more useful as you write more C++, so feel free to explore them as you go. +C++ provides several other operators that don't fit into the previous groups but are essential for various programming +tasks. These become more useful as you write more C++, so feel free to explore them as you go. ### Conditional Operator (`? :`) -The conditional operator (informally called the "ternary operator") is a shorthand for `if-else` statements. It takes three operands. +The conditional operator (informally called the "ternary operator") is a shorthand for `if-else` statements. It takes +three operands. **Syntax:** `condition ? value_if_true : value_if_false` @@ -420,7 +433,8 @@ ptr->y = 40; ### Address-Of and Dereference Operators -The address-of operator (`&`) returns the memory address of a variable, while the dereference operator (`*`) accesses the value at a given address. +The address-of operator (`&`) returns the memory address of a variable, while the dereference operator (`*`) accesses +the value at a given address. ```c++ int value = 42; @@ -470,7 +484,8 @@ the kind of conversion explicit. ## Experimenting with Operators -As you continue learning C++, you'll discover that operators can be used in creative and powerful ways. Some of these areas are operator overloading, combining operators, bitwise tricks, etc. +As you continue learning C++, you'll discover that operators can be used in creative and powerful ways. Some of these +areas are operator overloading, combining operators, bitwise tricks, etc. The best way to master operators is through practice. Try: @@ -479,4 +494,5 @@ The best way to master operators is through practice. Try: - Implementing algorithms that leverage bitwise operations - Creating your own classes with overloaded operators -We highly recommend writing code that is clear and readable to others (and your future self) and avoid clever tricks that can make code harder to read. +We highly recommend writing code that is clear and readable to others (and your future self) and avoid clever tricks +that can make code harder to read.