C++

C++ Operators – What is, Types and Programs

Operator denotes a course of action. As an illustration, the operator + stands for addition. An operator creates an output after working with two or more operands. As an illustration, the 3+4+5 here + operator operates on three operands and yields the result 12.

Operator tpes in C++

1) Basic Arithmetic Operators
2) Assignment Operators
3) Auto-increment and Auto-decrement Operators
4) Logical Operators
5) Comparison (relational) operators
6) Bitwise Operators
7) Ternary Operator

1) Basic Arithmetic Operators

Basic arithmetic operators are: +, -, *, /, %
+ is for addition.

 is for subtraction.

* is for multiplication.

/ is for division.

% is for modulo.
Note: The modulo operator yields the remainder; for instance, 20% of 5 would provide 0.

Example of Arithmetic Operators

#include <iostream>
using namespace std;
int main(){
  int num1 = 240;
  int num2 = 40;
  cout<<"num1 + num2: "<<(num1 + num2)<<endl;
  cout<<"num1 - num2: "<<(num1 - num2)<<endl;
  cout<<"num1 * num2: "<<(num1 * num2)<<endl;
  cout<<"num1 / num2: "<<(num1 / num2)<<endl;
  cout<<"num1 % num2: "<<(num1 % num2)<<endl;
  return 0;
}

Output:

num1 + num2: 280
num1 - num2: 200
num1 * num2: 9600
num1 / num2: 6
num1 % num2: 0

2) Assignment Operators

Assignments operators in C++ are: =, +=, -=, *=, /=, %=

num2 = num1 would assign value of variable num1 to the variable.

num2+=num1 is equal to num2 = num2+num1

num2-=num1 is equal to num2 = num2-num1

num2*=num1 is equal to num2 = num2*num1

num2/=num1 is equal to num2 = num2/num1

num2%=num1 is equal to num2 = num2%num1

Example of Assignment Operators

#include <iostream>
using namespace std;
int main(){
 int num1 = 240;
 int num2 = 40;
 num2 = num1;
 cout<<"= Output: "<<num2<<endl;
 num2 += num1;
 cout<<"+= Output: "<<num2<<endl;
 num2 -= num1;
 cout<<"-= Output: "<<num2<<endl;
 num2 *= num1;      
 cout<<"*= Output: "<<num2<<endl;
 num2 /= num1;      
 cout<<"/= Output: "<<num2<<endl;
 num2 %= num1;      
 cout<<"%= Output: "<<num2<<endl;
 return 0;
}

Output:

= Output: 240
+= Output: 480
-= Output: 240
*= Output: 57600
/= Output: 240
%= Output: 0

3) Auto-increment and Auto-decrement Operators

++ and —
num++ is equivalent to num=num+1;

num–- is equivalent to num=num-1;

Example of Auto-increment and Auto-decrement Operators

#include <iostream>
using namespace std;
int main(){
  int num1 = 240;
  int num2 = 40;
  num1++; num2--;
  cout<<"num1++ is: "<<num1<<endl;
  cout<<"num2-- is: "<<num2;
  return 0;
}

Output:

num1++ is: 241
num2-- is: 39

4) Logical Operators

Binary variables are utilized with logical operators. They are primarily utilized in loops and conditional expressions to evaluate conditions.

In C++, the logical operators are: &&, ||, and!

Say we have the boolean variables b1 and b2 in our system.

If both b1 and b2 are true, b1&&b2 will return true; otherwise, it will return false.

If both b1 and b2 are false, b1||b2 will return false; otherwise, it will return true.

If b1 is true, then b1 would return false, and if b1 is false, then b1 would return the reverse of b1.

An illustration of a logical operator

#include <iostream>
using namespace std;
int main(){
   bool b1 = true;
   bool b2 = false;
   cout<<"b1 && b2: "<<(b1&&b2)<<endl;
   cout<<"b1 || b2: "<<(b1||b2)<<endl;
   cout<<"!(b1 && b2): "<<!(b1&&b2);
   return 0;
}

Output:

b1 && b2: 0
b1 || b2: 1
!(b1 && b2): 1

5) Relational operators

We have six relational operators in C++: ==, !=, >, <, >=, <=

== returns true if both the left side and right side are equal

!= returns true if left side is not equal to the right side of operator.

> returns true if left side is greater than right.

< returns true if left side is less than right side.

>= returns true if left side is greater than or equal to right side.

<= returns true if left side is less than or equal to right side.

A relational operator example

#include <iostream>
using namespace std;
int main(){
   int num1 = 240;
   int num2 =40;
   if (num1==num2) {
      cout<<"num1 and num2 are equal"<<endl;
   }
   else{
      cout<<"num1 and num2 are not equal"<<endl;
   }
   if( num1 != num2 ){
      cout<<"num1 and num2 are not equal"<<endl;
   }
   else{ 
      cout<<"num1 and num2 are equal"<<endl;
   }
   if( num1 > num2 ){
      cout<<"num1 is greater than num2"<<endl;
   }
   else{
      cout<<"num1 is not greater than num2"<<endl;
   }
   if( num1 >= num2 ){ 
      cout<<"num1 is greater than or equal to num2"<<endl;
   }
   else{
      cout<<"num1 is less than num2"<<endl;
   }
   if( num1 < num2 ){
      cout<<"num1 is less than num2"<<endl;
   }
   else{
      cout<<"num1 is not less than num2"<<endl;
   }
   if( num1 <= num2){
      cout<<"num1 is less than or equal to num2"<<endl;
   }
   else{
      cout<<"num1 is greater than num2"<<endl;
   }
   return 0;
}

Output:

num1 and num2 are not equal
num1 and num2 are not equal
num1 is greater than num2
num1 is greater than or equal to num2
num1 is not less than num2
num1 is greater than num2

6) Bitwise Operators

There are six bitwise Operators: &, |, ^, ~, <<, >>

num1 = 11; /* equal to 00001011*/
num2 = 22; /* equal to 00010110 */

num1 & num2 compares the equivalent bits in numbers num1 and num2, returning 1 if the two bits are equal and 0 otherwise. Because only the second last bit of num1 and num2’s binary representations match, it would yield 2 in our example, which is 00000010.

num1 | num2 matching bits of num1 and num2 are compared, and if either bit is 1, 1 is produced; otherwise, 0 is returned. In this instance, it would yield 31, or 00011111.

num1 ^ num2 matching bits of num1 and num2 are compared, and if they are not same, creates 1 otherwise returns 0. It would return 29 in our case, which is identical to 00011101

~num1merely flips the bit from 0 to 1 and 1 to 0, acting as a complement operator In our example, it would yield -12, which is equivalent to 11110100 in signed 8-bit format.

num1 << 2 is a left shift operator that shifts the bits to the left, gets rid of the far-left bit, and sets the rightmost bit’s value to 0. In this instance, the output is 44, which is equal to 00101100.

Note that bits are moved two positions to the left side in the example below since we provided 2 on the right side of the shift operator. If we changed this value, the amount of bits supplied on the operator’s right side would be moved. Same goes for the operator on the right side.

num1 >> 2 is a right shift operator that shifts the bits to the right, discards the far-right bit, and values the leftmost bit to 0 (zero). In this instance, the output is 2, which is equal to 00001010.

Example of Bitwise Operators

#include <iostream>
using namespace std;
int main(){
   int num1 = 11;  /* 11 = 00001011 */
   int num2 = 22;  /* 22 = 00010110 */ 
   int result = 0;
   result = num1 & num2;
   cout<<"num1 & num2: "<<result<<endl;
   result = num1 | num2;
   cout<<"num1 | num2: "<<result<<endl;
   result = num1 ^ num2;
   cout<<"num1 ^ num2: "<<result<<endl;
   result = ~num1;
   cout<<"~num1: "<<result<<endl;
   result = num1 << 2;
   cout<<"num1 << 2: "<<result<<endl;
   result = num1 >> 2;
   cout<<"num1 >> 2: "<<result;
   return 0;
}

Output:

num1 & num2: 2
num1 | num2: 31
num1 ^ num2: 29
~num1: -12
num1 << 2: 44 num1 >> 2: 2

7) Ternary Operator

This operator determines the value by evaluating a boolean expression.

Syntax:

variable num1 = (expression) ? value if true : value if false

If the expression results true then the first value before the colon (:) is assigned to the variable num1 else the second value is assigned to the num1.

Example of Ternary Operator

#include <iostream>
using namespace std;
int main(){
  int num1, num2; num1 = 99;
  /* num1 is not equal to 10 that's why
   * the second value after colon is assigned
   * to the variable num2
   */
  num2 = (num1 == 10) ? 100: 200;
  cout<<"num2: "<<num2<<endl;
  /* num1 is equal to 99 that's why
   * the first value is assigned
   * to the variable num2
   */
  num2 = (num1 == 99) ? 100: 200;
  cout<<"num2: "<<num2;
  return 0;
}

Output:

num2: 200
num2: 100

Miscellaneous Operators

Other operators in C++ include the sizeof operator and the Comma operator. In a different tutorial, we will go through them in greater detail.

In C++, operator precedence

When an expression has more than one operator, this indicates which operator needs to be evaluated first. higher precedence operators at the top and lower precedence operators at the bottom.

Unary Operators
++ – – ! ~

Multiplicative
* / %

Additive
+ –

Shift
<< >> >>>

Relational
> >= < <=

Equality
== !=

Bitwise AND

&
Bitwise XOR
^

Bitwise OR

|

Logical AND
&&

Logical OR
||

Ternary
?:

Assignment
= += -= *= /= %= > >= < <= &= ^= |=

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button