DEV Community

Cover image for Operators and Expression
Zaynul Abedin Miah
Zaynul Abedin Miah

Posted on • Updated on

Operators and Expression

Arithmetic Operators

Arithmetic Operators in C++ are used to perform arithmetic or mathematical operations on the operands. For example, ‘+’ is used for addition, ‘–‘ is used for subtraction, ‘*’ is used for multiplication, etc. In simple terms, arithmetic operators are used to perform arithmetic operations on variables and data; they follow the same relationship between an operator and an operand.

// C++ program to execute all 5
// arithmetic function

#include <iostream>
using namespace std;

int main()
{
    int a, b;
    a = 10;
    b = 3;

    // printing the sum of a and b
    cout<< "a + b= " << (a + b) << endl;

    // printing the difference of a and b
    cout << "a - b = " << (a - b) << endl;

    // printing the product of a and b
    cout << "a * b = " << (a * b) << endl;

    // printing the division of a by b
    cout << "a / b = " << (a / b) << endl;

    // printing the modulo of a by b
    cout << "a % b = " << (a % b) << endl;

    return 0;
}


Enter fullscreen mode Exit fullscreen mode

Output:
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1

Assignment Operators

Assignment operators are used to assigning value to a variable.
Different types of assignment operators are shown below:

  • “=”: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.

  • “+=”: This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.

  • “-=”This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left.

  • =”This operator is combination of ‘’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.

  • “/=”This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.

Example:

#include <iostream>
using namespace std;

int main() 
{ 

    // Assigning value 10 to a 
    // using "=" operator 
    int a = 10; 
    cout << "Value of a is "<<a<<"\n"; 

    // Assigning value by adding 10 to a 
    // using "+=" operator 
    a += 10; 
    cout << "Value of a is "<<a<<"\n"; 

    // Assigning value by subtracting 10 from a 
    // using "-=" operator 
    a -= 10; 
    cout << "Value of a is "<<a<<"\n"; 

    // Assigning value by multiplying 10 to a 
    // using "*=" operator 
    a *= 10; 
    cout << "Value of a is "<<a<<"\n"; 

    // Assigning value by dividing 10 from a 
    // using "/=" operator 
    a /= 10; 
    cout << "Value of a is "<<a<<"\n"; 

    return 0; 
}
Enter fullscreen mode Exit fullscreen mode

Output:
Value of a is 10
Value of a is 20
Value of a is 10
Value of a is 100
Value of a is 10

Increment and Decrement

The increment operator ++ adds 1 to its operand, and the decrement operator -- subtracts 1 from its operand. So, x = x+1; is the same as x++; And similarly, x = x-1; is the same as x--.

Example:

int a = 10;
int b = 3;

cout<<"Increment Decrement Operators" <<endl;
++a;
++b;

cout << "A " << a << endl;
cout << "B " << b << endl;

int x = 10;
int y = x++;
cout <<"X , Y "<<x<<","<<y <<endl;
int z = ++x;
cout <<"X, Z " << x <<", "<<z <<endl;
Enter fullscreen mode Exit fullscreen mode

Output:
A 11
B 2
X , Y 11,10
X, Z 12,12

Relational Operators

A relational operator is used to check the relationship between two operands. For example,
Example:

int a = 10;
int b = 3;
cout << "Relational Operators" <<endl;
cout << (a>b) <<endl; 
Enter fullscreen mode Exit fullscreen mode

Output:
1

Here, > is a relational operator. It checks if a is greater than b or not.

If the relation is true, it returns 1 whereas if the relation is false, it returns 0.

The following table summarizes the relational operators used in C++.

Image description

Logical Operators

Image description

Truth Table of && Operator
Let a and b be two operands. 0 represents false while 1 represents true. Then,
Image description

Example:

// C++ program demonstrating && operator truth table

#include <iostream>
using namespace std;

int main() {
    int a = 5;
    int b = 9;
    // true && false = false
    cout << ((a == 5) && (a > b)) << endl;

    // true && true = true
    cout << ((a == 5) && (a < b)) << endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:
0
1

OR Operator
Truth Table of || Operator

Image description

Example:
// C++ program demonstrating || operator truth table

include

using namespace std;

int main() {
int a = 5;
int b = 9;

// false && false = false
cout << ((a == 0) || (a > b)) << endl;

// false && true = true
cout << ((a == 0) || (a < b)) << endl;

// true && false = true
cout << ((a == 5) || (a > b)) << endl;

// true && true = true
cout << ((a == 5) || (a < b)) << endl;

return 0;
Enter fullscreen mode Exit fullscreen mode

}

**Output:**
`0
1
1
1`

**NOT Operator !**
Reverse the result, returns false if the result is true.

**Example:**

Enter fullscreen mode Exit fullscreen mode

// C++ program demonstrating ! operator truth table

include

using namespace std;

int main() {
int a = 5;

// !false = true
cout << !(a == 0) << endl;

// !true = false
cout << !(a == 5) << endl;

return 0;
Enter fullscreen mode Exit fullscreen mode

}



**Output:**
`1
0`

## Bitwise Operators

**Binary And &**
It takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.  
_Rules:_
0 & 0 = 0
1 & 0 = 0
0 & 1 = 0
1 & 1 = 1

**Binary OR |**
It takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.
_Rules:_
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0
1 | 1 = 1

**Binary XOR ^ (Exclusive OR)**
It takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
_Rules:_
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

**Binary Not ~**
It takes one number and inverts all bits of it.
~0 = 1
~1 = 0 

**Binary Left Shift**It takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift. 

Enter fullscreen mode Exit fullscreen mode

Top comments (0)