Operators in C++

Operators in C++

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++:

Operator

Description

+

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++:

Operator

Description

\==

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++:

Operator

Description

&&

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; //

We appreciate you reading our blog, and we hope to welcome you again soon for more interesting posts. We appreciate your participation in our community and look forward to hearing from you.