AlgoMaster Logo

Operators

Last Updated: January 3, 2026

8 min read

Operators allow you to manipulate data, perform arithmetic, and make logical decisions, essentially helping you bring your code to life.

They are the tools you’ll use to work with variables and constants as you create functional programs.

Arithmetic Operators

Arithmetic operators are the most common type. They perform basic mathematical operations like addition, subtraction, multiplication, and division.

Here’s a quick rundown of the key arithmetic operators in C++:

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulus (remainder of division)

Let’s see how each of these works in practice.

In this example, we declare two integers, a and b, and then perform various arithmetic operations. It’s good to note that when dividing two integers, the result is also an integer. This means any decimal part is truncated, which can lead to unexpected results if you’re not careful.

Relational Operators

Next up are relational operators, which help you compare values. They return a boolean value (true or false) based on the comparison. Here are the main relational operators:

  • == : Equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to

Let’s see these in action:

The output here is 1 for true and 0 for false. These operators become extremely useful when you need to make decisions in your code, like in if statements or loops.

Logical Operators

Logical operators are used to combine multiple boolean expressions. They help you create complex conditions, especially in control flow statements like if, while, or for. The three main logical operators in C++ are:

  • && : Logical AND
  • || : Logical OR
  • ! : Logical NOT

Here’s how they work:

In this example, && returns true only if both operands are true. The || operator returns true if at least one operand is true, while ! negates the boolean value.

Bitwise Operators

Bitwise operators operate on bits and perform bit-level operations. Though they might seem less common, they’re incredibly powerful for specific applications like low-level programming, graphics, and performance optimization. Here’s a breakdown:

  • & : Bitwise AND
  • | : Bitwise OR
  • ^ : Bitwise XOR
  • ~ : Bitwise NOT
  • << : Left shift
  • >> : Right shift

Let’s look at an example:

In this code, the binary representations of a and b illustrate how bitwise operations work. For instance, a & b compares each corresponding bit: if both bits are 1, the result bit is 1, otherwise it’s 0.

Assignment Operators

Assignment operators are used to assign values to variables. The most basic is the simple assignment operator =, but there are several others that combine assignment with arithmetic and bitwise operations:

  • = : Simple assignment
  • += : Add and assign
  • -= : Subtract and assign
  • *= : Multiply and assign
  • /= : Divide and assign
  • %= : Modulus and assign
  • &= : Bitwise AND and assign
  • |= : Bitwise OR and assign
  • ^= : Bitwise XOR and assign
  • <<= : Left shift and assign
  • >>= : Right shift and assign

Here’s an example that shows off some of these:

Using compound assignment operators like += makes your code more concise and often more readable. It also reduces the chance of mistakes when you’re updating values.

Miscellaneous Operators

C++ has a few additional operators that may not fit neatly into the categories above but are just as important:

  1. Ternary Operator (? :): This is a shorthand for an if-else statement.
  2. Comma Operator (,): It evaluates multiple expressions in a single statement, returning the value of the last expression.
  3. Sizeof Operator: It returns the size of a data type or variable in bytes.

Here’s a quick look at how some of these work:

The ternary operator is a great way to simplify conditional assignments, while the comma operator can help streamline multiple updates in a single line.