Skip to main content

Command Palette

Search for a command to run...

Operators in CPP

Updated
3 min readView as Markdown
Operators in CPP

C++ is a powerful programming language that provides several operators to manipulate data. Operators are symbols or keywords that perform specific operations on one or more operands. In this blog, we will explore the different types of operators in C++.

1) Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, and modulus. The following table shows the arithmetic operators in C++:

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

Example:

int a = 10, b = 3;
cout << a + b << endl; // Output: 13
cout << a - b << endl; // Output: 7
cout << a * b << endl; // Output: 30
cout << a / b << endl; // Output: 3
cout << a % b << endl; // Output: 1

2) Relational Operators

Relational operators are used to compare two values and return a Boolean value (true or false). The following table shows the relational operators in C++:

OperatorDescription
\==Equal to
!=Not equal to
\>Greater than
<Less than
\>=Greater than or equal to
<=Less than or equal to

Example:

int a = 10, b = 3;
cout << (a == b) << endl; // Output: 0 (false)
cout << (a != b) << endl; // Output: 1 (true)
cout << (a > b) << endl; // Output: 1 (true)
cout << (a < b) << endl; // Output: 0 (false)
cout << (a >= b) << endl; // Output: 1 (true)
cout << (a <= b) << endl; // Output: 0 (false)

3) Logical Operators

Logical operators are used to combine two or more conditions and return a Boolean value. The following table shows the logical operators in C++:

OperatorDescription
&&Logical AND
Logical OR
!Logical NOT

Example:

int a = 10, b = 3, c = 5;
cout << ((a > b) && (a > c)) << endl; // Output: 1 (true)
cout << ((a < b) || (a < c)) << endl; // Output: 0 (false)
cout << !(a > b) << endl; // Output: 0 (false)

4) Bitwise Operators

Bitwise operators are used to manipulate the bits of a value. The following table shows the bitwise operators in C++:

| Operator |
Description
| | --- | --- | | & | Bitwise AND | | | | Bitwise OR | | ^ | Bitwise XOR | | ~ | Bitwise NOT | | << | Bitwise left shift | | \>> | Bitwise right shift |

Example:

int a = 5, b = 3;
cout << (a & b) << endl; // Output: 1 (binary 001 & 011 = 001)
cout << (a \| b) << endl; // Output: 7 (binary 101 \| 011 = 111)
cout << (a ^ b) << endl; // Output: 6 (binary 101 ^ 011 = 110)
cout << (~a) << endl; //
S

You are helping the students like me by explaining very well....

T

Great work. Concept explained really good.

V

Learning Operators in C++ made easy !!!

More from this blog

C++ Concepts:Everything You Need to Know

26 posts