Operators
Introduction
In C++, an operator is a special symbol that is used to perform specific operations on one, two, or three operands (data). C++ provides a rich set of operators which can be classified into the following types:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators
Let's take a closer look at these categories.
Arithmetic Operators
Arithmetic Operators are used to perform mathematical calculations. They include:
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
) - Modulus Operator (
%
) - Increment Operator (
++
) - Decrement Operator (
--
)
int a = 10, b = 4;
cout << "a + b = " << a + b;
cout << "a - b = " << a - b;
cout << "a * b = " << a * b;
cout << "a / b = " << a / b;
cout << "a % b = " << a % b;
Relational Operators
Relational Operators are used to compare two values. They include:
- Equal to (
==
) - Not equal to (
!=
) - Greater than (
>
) - Less than (
<
) - Greater than or equal to (
>=
) - Less than or equal to (
<=
)
int a = 10, b = 4;
cout << "(a == b) = " << (a == b);
cout << "(a != b) = " << (a != b);
cout << "(a > b) = " << (a > b);
cout << "(a < b) = " << (a < b);
cout << "(a >= b) = " << (a >= b);
cout << "(a <= b) = " << (a <= b);
Logical Operators
Logical Operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. They include:
- Logical AND (
&&
) - Logical OR (
||
) - Logical NOT (
!
)
int a = 10, b = 4, c = 20;
cout << ((a > b) && (b < c));
cout << ((a > b) || (b > c));
cout << (!(a > b));
Bitwise Operators
Bitwise operators are used to perform bitwise operations on bits. They include:
- Bitwise AND (
&
) - Bitwise OR (
|
) - Bitwise XOR (
^
) - Bitwise NOT (
~
) - Left shift (
<<
) - Right shift (
>>
)
int a = 10, b = 4;
cout << "a & b = " << (a & b);
cout << "a | b = " << (a | b);
cout << "a ^ b = " << (a ^ b);
cout << "~a = " << (~a);
cout << "a << b = " << (a << b);
cout << "a >> b = " << (a >> b);
Assignment Operators
C++ provides a set of assignment operators to assign a value to a variable. They include:
- Simple assignment (
=
) - Add and assignment (
+=
) - Subtract and assignment (
-=
) - Multiply and assignment (
*=
) - Divide and assignment (
/=
) - Modulo and assignment (
%=
) - Left shift and assignment (
<<=
) - Right shift and assignment (
>>=
) - Bitwise AND and assignment (
&=
) - Bitwise XOR and assignment (
^=
) - Bitwise OR and assignment (
|=
)
int a = 10;
a += 20;
cout << "a = " << a;
Miscellaneous Operators
Some other important operators in C++ include:
- Sizeof operator
- Conditional operator (Ternary operator)
- Comma operator
- Pointer operators (dereference and address-of operators)
int a = 10;
cout << "Size of a = " << sizeof(a);
cout << "a is " << ((a > 5) ? "greater" : "smaller") << " than 5";
Understanding and using operators correctly is fundamental to building effective C++ applications. Through practice, you will become more comfortable with using these operators to perform a variety of tasks in your code.