Operators¶
In programming, operators are special symbols or keywords that perform operations on values (called operands). These operations can range from arithmetic and logic to memory access and control flow. Operators are essential for creating expressions that calculate, compare, and manipulate data.
Note
This sounds just like functions in programming, which we'll dive into the next lesson.
That's because operators are functions.
Categories¶
Category | Purpose | Example |
---|---|---|
Arithmetic | Perform basic math | + , - , * , / , % |
Assignment | Assign values | = , += , -= , *= |
Comparison | Compare two values (returns bool) | == , != , < , > , <= , >= |
Logical | Combine or negate boolean expressions | && |
Bitwise | Perform bit-level operations | & |
Increment/Decrement | Increase or decrease a value by one | ++ , -- |
C/C++: Pointer/Dereference | Work with memory addresses | & , * |
Member access | Access object or struct members | . , -> |
Ternary | Compact conditional logic | ? : |
Type-related | Type casting or checking | static_cast , is |
Arithmetic Operators¶
Example
Assignment Operators¶
Comparison Operators¶
Logical Operators¶
Bitwise Operators¶
Operate at the binary level. Often used in low-level code, hardware interaction, or performance-critical systems.
Operator | Name | Description |
---|---|---|
& |
AND | Both bits must be 1 |
| |
OR | Either bit is 1 |
^ |
XOR | One bit is 1, not both |
~ |
NOT | Inverts bits (1's complement) |
<< |
Shift Left | Moves bits left |
>> |
Shift Right | Moves bits right |
Example
Increment / Decrement¶
Note
Postfix (a++) vs Prefix (++a) can have different effects in expressions.
C/C++: Pointer and Dereference¶
Example
Ternary Operator¶
Compact alternative to an if-else.
Operator Precedence¶
Operators are evaluated in a specific order. Use parentheses () to control precedence.