Operators

Arithmetic, logical, and bitwise operators

Interview Relevant: Essential for problem solving

C++ Operators

Operators are symbols that perform operations on operands.

Operator Categories

  • Arithmetic: + - * / % ++ --
  • Comparison: == != < > <= >= <=> (C++20)
  • Logical: && || !
  • Bitwise: & | ^ ~ << >>
  • Assignment: = += -= *= /= %= &= |= ^= <<= >>=
  • Member Access: . -> :: .* ->*

Code Examples

Examples of arithmetic, bitwise, and logical operators.

cpp
1int a = 10, b = 3;
2int sum = a + b;      // 13
3int mod = a % b;      // 1
4
5// Bitwise operations
6int x = 5;            // 0101 in binary
7int y = 3;            // 0011 in binary
8int andResult = x & y; // 0001 = 1
9int orResult = x | y;  // 0111 = 7
10int xorResult = x ^ y; // 0110 = 6
11int leftShift = x << 1; // 1010 = 10
12
13// Logical operators
14bool result = (a > 5) && (b < 5);  // true

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In