C++ Operators

C++ Operators are symbols that take one or two operands, perform a specific action on these operands, and return the result.

Operators can be classified into following groups.

In this tutorial, we shall go through each of these Operators, and also list out individual tutorials that describe respective operators in detail.

Assignment Operators

Arithmetic operators are used to perform common operations like addition, subtraction, multiplication, division, etc.

The following table specifies symbol, example, and description for each of the Assignment Operator in C++.

ArithmeticOperationOperator SymbolExampleDescription
Simple Assignment=x = 2Assign x with 2.
Addition Assignment+=x += 3Add 3 to the value of x and assign the result to x.
Subtraction Assignment-=x -= 3Subtract 3 from x and assign the result to x.
Multiplication Assignment*=x *= 3Multiply x with 3 and assign the result to x.
Division Assignment/=x /= 3Divide x with 3 and assign the quotient to x.
Remainder Assignment%=x %= 3Divide x with 3 and assign the remainder to x.
Bitwise AND Assignment&=x &= 3Perform x & 3 and assign the result to x.
Bitwise OR Assignment|=x |= 3Perform x | 3 and assign the result to x.
Bitwise-exclusive-OR Assignment^=x ^= 3Perform x ^ 3 and assign the result to x.
Left-shift Assignment<<=x <<= 3Left-shift the value of x by 3 places and assign the result to x.
Right-shift Assignment>>=x >>= 3Right-shift the value of x by 3 places and assign the result to x.

The following are the list of detailed tutorials for Arithmetic Operators.

Arithmetic Operators

Arithmetic operators are used to perform common operations like addition, subtraction, multiplication, division, etc.

ArithmeticOperationOperatorSymbolDescriptionExample
Addition+Returns addition of two operands.x + y
SubtractionReturns difference of second operand from first operand.x – y
Multiplication*Returns the product of two operands.x * y
Division/Returns the quotient of division of first operand by second operand.x / y
Modulus%Returns the remainder of division of first operand by second operand.x % y
Increment++Increments the value of variable by 1.++x
DecrementDecrements the value of variable by 1.–x

The following are the list of detailed tutorials for Arithmetic Operators.

Logical Operators

Logical operators are used to perform boolean operations like AND, OR, and NOT.

LogicalOperationOperatorSymbolExampleDescription
AND&&x && yReturns AND between x and y.
OR||x || yReturns OR between x and y.
NOT!!x Returns negation of x.

The following are the list of detailed tutorials for Logical Operators.

Relational Operators

Relational operators are used to compare given values. These are also called Comparison Operators.

RelationalOperationOperator SymbolExampleDescription
Equal To==x == yReturns true if x is equal to y, else it returns false.
Not Equal!=x != yReturns true if x is not equal to y, else it returns false.
Greater Than>x > yReturns true if x is greater than y, else it returns false.
Less Than<x < yReturns true if x is less than y, else it returns false.
Greater Than or Equal To>=x >= yReturns true if x is greater than or equal to y, else it returns false.
Less Than or Equal To<=x <= yReturns true if x is less than or equal to y, else it returns false.

The following are the list of detailed tutorials for Logical Operators.

Bitwise Operators

C++ Bitwise Operators are used to perform bitwise operations on integer or char operands. Bitwise operations are done at bit level, meaning, operations like AND, OR, XOR, etc., are done between respective bits of the operands.

The following table specifies symbol, example, and description for each of the Bitwise Operator in C++.

BitwiseOperationOperator SymbolExampleDescription
AND&x & yReturns the bitwise AND operation between x and y.
OR|x | yReturns the bitwise OR operation between x and y.
XOR^x ^ yReturns the bitwise XOR operation between x and y.
Complement~~xReturns the complement of x.
Left Shift<<x << yReturns the result of x left shifted by y number of places.
Right Shift>>x >> yReturns the result of x right shifted by y number of places.

The following are the list of detailed tutorials for Logical Operators.

Conclusion

In this C++ Tutorial, we learned about different types of operators in C++.