Last Updated: January 3, 2026
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 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.
To get a floating-point result from a division, at least one operand should be a floating-point number. You can achieve this by casting:
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 toLet’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.
Be careful with the equality operator == versus the assignment operator =. It's a common source of bugs when = is mistakenly used in place of ==.
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 NOTHere’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.
Logical operators can be short-circuited. This means if the first condition of an && operation is false, the second condition is not evaluated since the entire expression can't be true. This can save time in complex conditions.
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 shiftLet’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.
The left and right shift operators can manipulate data efficiently for tasks like multiplying or dividing by powers of two. For example, a << 1 is equivalent to multiplying a by 2.
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 assignHere’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.
C++ has a few additional operators that may not fit neatly into the categories above but are just as important:
? :): This is a shorthand for an if-else statement.,): It evaluates multiple expressions in a single statement, returning the value of the last expression.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.
While the comma operator can be handy, use it sparingly. Code readability can suffer if overused.