Arithmetic, comparison, logical, increment/decrement, compound assignment, bitwise operators, and std::bitset.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 5 + 3 → 8 |
| - | Subtraction | 5 - 3 → 2 |
| * | Multiplication | 5 * 3 → 15 |
| / | Division | 5 / 2 → 2 (integer division!) |
| % | Modulo (remainder) | 5 % 2 → 1 |
5 / 2 is 2, not 2.5. To get a floating-point result, cast at least one operand: (double)5 / 2 → 2.5.| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| < | Less than |
| > | Greater than |
| <= | Less than or equal |
| >= | Greater than or equal |
| Operator | Meaning |
|---|---|
| && | AND — both sides must be true |
| || | OR — at least one side must be true |
| ! | NOT — inverts a boolean |
C++ stops evaluating a logical expression as soon as the result is determined:
&&: if the left side is false, the right side is never evaluated||: if the left side is true, the right side is never evaluatedint* ptr = nullptr;
if (ptr != nullptr && *ptr == 5) { // safe — *ptr never reached if ptr is null
// ...
}
Both ++ and -- come in two forms that differ in when the update happens relative to the expression being evaluated:
The current value is used in the expression first, then the variable is incremented. Requires a temporary copy — slightly less efficient.
The variable is incremented first, then the new value is used. No temporary copy needed — preferred in loops and templates.
int a = 5;
int b = a++; // b = 5, a = 6 (post: b gets old value)
int c = ++a; // c = 7, a = 7 (pre: c gets new value)
++i over i++ in loop counters — the compiler often optimizes them identically for primitive types, but the habit matters for iterators and custom types.Shorthand for applying an operator and assigning the result back to the same variable:
| Operator | Equivalent |
|---|---|
| x += 3 | x = x + 3 |
| x -= 3 | x = x - 3 |
| x *= 3 | x = x * 3 |
| x /= 3 | x = x / 3 |
| x %= 3 | x = x % 3 |
| x &= 3 | x = x & 3 |
| x |= 3 | x = x | 3 |
| x ^= 3 | x = x ^ 3 |
| x <<= 1 | x = x << 1 |
| x >>= 1 | x = x >> 1 |
Bitwise operators work directly on the individual bits of integer types.
| Operator | Name | Operands | Notes |
|---|---|---|---|
| ~ | NOT | One | Flips all bits |
| & | AND | Two | 1 only if both bits are 1 |
| | | OR | Two | 1 if either bit is 1 |
| ^ | XOR | Two | 1 only if bits differ |
| << | Shift Left | Two | Each shift multiplies by 2 |
| >> | Shift Right | Two | Each shift divides by 2 |
unsigned types for bit manipulation.
unsigned int x = 0b0000'1010; // 10
x << 1; // 0b0001'0100 = 20 (×2)
x >> 1; // 0b0000'0101 = 5 (÷2)
~x; // 0b1111'0101 = 245 (all bits flipped)
std::bitsetstd::bitset<N> is a fixed-size sequence of bits. The template parameter N is the number of bits.
#include <bitset>
std::bitset<8> flags = 5; // 00000101 (binary of 5)
flags[0]; // 1 — LSB (least significant bit)
flags[7]; // 0 — MSB
flags.count(); // 2 — number of set bits
flags.flip(2); // flip bit at position 2
std::cout << flags; // prints "00000101"
coutcount(), flip(), any(), all()