C++ Operators | A Comprehensive Guide to C++ Operators: Types, Examples, and Precedence

Learn about the different types of operators in C++, including arithmetic, relational, logical, bitwise, and assignment operators, as well as their functions and operator precedence. Improve your C++ programming skills by mastering these fundamental concepts.

C++ Operators | A Comprehensive Guide to C++ Operators: Types, Examples, and Precedence
C++ Operators | A Comprehensive Guide to C++ Operators: Types, Examples, and Precedence

Introduction

Operators are essential elements of any programming language, and C++ is no exception. In C++, operators are symbols or keywords that are used to perform operations on operands. In this article, we will explore the different types of operators available in C++, including arithmetic, relational, logical, bitwise, and assignment operators.

Arithmetic Operators:

Arithmetic operators are used to perform arithmetic operations on numeric operands. The available arithmetic operators in C++ are:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)

Example:

int x = 10;
int y = 5;
int sum = x + y; // 15
int difference = x - y; // 5
int product = x * y; // 50
int quotient = x / y; // 2
int remainder = x % y; // 0

Relational Operators:

Relational operators are used to compare two operands and return a Boolean value. The available relational operators in C++ are:

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

Example:

int x = 10;
int y = 5;
bool equal = (x == y); // false
bool notEqual = (x != y); // true
bool greaterThan = (x > y); // true
bool lessThan = (x < y); // false
bool greaterThanOrEqual = (x >= y); // true
bool lessThanOrEqual = (x <= y); // false

Logical Operators:

Logical operators are used to perform logical operations on Boolean operands. The available logical operators in C++ are:

  • Logical AND (&&)
  • Logical OR (||)
  • Logical NOT (!)

Example:

bool x = true;
bool y = false;
bool andResult = (x && y); // false
bool orResult = (x || y); // true
bool notResult = !x; // false

Bitwise Operators:

Bitwise operators are used to perform operations on individual bits of operands. The available bitwise operators in C++ are:

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

Example:

int x = 5; // 0101 in binary
int y = 3; // 0011 in binary
int andResult = x & y; // 0001 in binary (1 in decimal)
int orResult = x | y; // 0111 in binary (7 in decimal)
int notResult = ~x; // 1010 in binary (-6 in decimal)
int xorResult = x ^ y; // 0110 in binary (6 in decimal)
int leftShiftResult = x << 1; // 1010 in binary (10 in decimal)
int rightShiftResult = x >> 1; // 0010 in binary (2 in decimal)

Assignment Operators:

Assignment operators are used to assign a value to a variable. The available assignment operators in C++ are:

  • Simple assignment (=)
  • Addition assignment (+=)
  • Subtraction assignment (-=)
  • Multiplication assignment (*=)
  • Division assignment (/=)
  • Modulus assignment (%=)
  • Bitwise AND assignment (&=)
  • Bitwise OR assignment (|=)
  • Bitwise XOR assignment (^=)
  • Left shift assignment (<<=)
  • Right shift assignment (>>=)

Example:

int x = 5;
x += 3; // equivalent to x = x + 3, x is now 8
x -= 2; // equivalent to x = x - 2, x is now 6
x *= 2; // equivalent to x = x * 2, x is now 12
x /= 3; // equivalent to x = x / 3, x is now 4
x %= 2; // equivalent to x = x % 2, x is now 0
x &= 3; // equivalent to x = x & 3, x is now 0
x |= 3; // equivalent to x = x | 3, x is now 3
x ^= 2; // equivalent to x = x ^ 2, x is now 1
x <<= 1; // equivalent to x = x << 1, x is now 2
x >>= 1; // equivalent to x = x >> 1, x is now 1

Operator Precedence:

Operator precedence determines the order in which operators are evaluated in an expression. In C++, operators with higher precedence are evaluated first. If two operators have the same precedence, the evaluation order is determined by the associativity of the operators. The table below shows the operator precedence and associativity in C++:

Precedence Operator Associativity
1 () [] -> . Left to right
2 ++ -- + - ! ~ * & sizeof Right to left
3 * / % Left to right
4 + - Left to right
5 << >> Left to right
6 < <= > >= Left to right
7 == != Left to right
8 & Left to right
9 ^ Left to right
10 | Left to right
11 && Left to right
12 || Left to right
13 ?: Right to left
14 = += -= *= /= %= &= ^= |= <<= >>= Right to left
15 , Left to right

Example:

int x = 5;
int y = 3;
int z = 2;

int result = x + y * z; // result is 11 (y * z is evaluated first due to higher precedence)

bool boolResult = x > y || y < z; // boolResult is true ((x > y) is evaluated first due to higher precedence)

Conclusion:

In conclusion, operators are a fundamental part of C++ programming, and an understanding of their types and functions is essential for writing efficient code. In this article, we have explored the different types of operators available in C++, including arithmetic, relational, logical, bitwise, and assignment operators, as well as operator precedence. By mastering these concepts, you will be well-equipped to write robust and efficient C++ code.