Operators
Quick Notes
Section titled “Quick Notes”Operator giúp tạo biểu thức từ biến và literal. Ưu tiên toán tử có thể gây bất ngờ, nên dùng ngoặc khi biểu thức dài. Bitwise operator rất hay dùng trong competitive programming.
Syntax
Section titled “Syntax”int sum = a + b;bool ok = x > 0 && x < 10;int mask = 1 << bit;Operator Groups
Section titled “Operator Groups”| Group | Operators | Example |
|---|---|---|
| Arithmetic | + - * / % | x % 2 == 0 |
| Comparison | == != < > <= >= | a <= b |
| Logical | `&& | |
| Assignment | = += -= *= /= %= | x += 3 |
| Bitwise | &, |, ^, ~, <<, >> | mask & (1 << i) |
| Ternary | ?: | x > 0 ? x : -x |
Example
Section titled “Example”int a = 10;int b = 3;
int div = a / b; // 3int mod = a % b; // 1bool inside = a >= 1 && a <= 20;int withBit = a | (1 << 2);int sign = a >= 0 ? 1 : -1;Common Mistakes
Section titled “Common Mistakes”a / bvới hai số nguyên sẽ trả về số nguyên.&&là logical AND, còn&là bitwise AND.1 << ioverflow nếuiquá lớn với kiểuint.
Search Keywords
Section titled “Search Keywords”operators toán tử arithmetic comparison logical assignment bitwise ternary and or shift modulo