In any programming language, operators play a vital role i.e. they create a foundation for the programming. Similarly, the functionality of Solidity is also incomplete without the use of operators. Operators allow users to perform different operations on operands. Solidity supports the following types of operators based upon their functionality.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment operators
- Conditional Operator
Arithmetic Operators
These operators are used to perform arithmetic or mathematical operations. Solidity supports the following arithmetic operators :
Operator |
Denotation |
Description |
Addition |
+ |
Used to add two operands |
Subtraction |
– |
Used to subtract the second operand from first |
Multiplication |
* |
Used to multiply both operands |
Division |
/ |
Used to divide numerator by denominator |
Modulus |
% |
Gives the remainder after integer division |
Increment |
++ |
Increases the integer value by one |
Decrement |
— |
Decreases the integer value by one |
Example: In the below example, the contract SolidityTest demonstrates the above mentioned different types of arithmetic operators.
Solidity
pragma solidity ^0.5.0;
contract SolidityTest {
uint16 public a = 20;
uint16 public b = 10;
uint public sum = a + b;
uint public diff = a - b;
uint public mul = a * b;
uint public div = a / b;
uint public mod = a % b;
uint public dec = --b;
uint public inc = ++a;
}
|
Output :

Relational Operators
These operators are used to compare two values. Solidity supports the following relational operators :
Operator |
Denotation |
Description |
Equal |
== |
Checks if two values are equal or not, returns true if equals, and vice-versa |
Not Equal |
!= |
Checks if two values are equal or not, returns true if not equals, and vice-versa |
Greater than |
> |
Checks if left value is greater than right or not, returns true if greater, and vice-versa |
Less than |
< |
Checks if left value is less than right or not, returns true if less, and vice-versa |
Greater than or Equal to |
>= |
Checks if left value is greater and equal than right or not, returns true if greater and equal, and vice-versa |
Less than or Equal to |
<= |
Checks if left value is less than right or not, returns true if less and equals, and vice-versa |
Example: In the below example, the contract SolidityTest demonstrates the above mentioned different types of relational operators.
Solidity
pragma solidity ^0.5.0;
contract SolidityTest {
uint16 public a = 20;
uint16 public b = 10;
bool public eq = a == b;
bool public noteq = a != b;
bool public gtr = a > b;
bool public les = a < b;
bool public gtreq = a >= b;
bool public leseq = a <= b;
}
|
Output :

Logical Operators
These operators are used to combine two or more conditions. Solidity supports the following arithmetic operators :
Operator |
Denotation |
Description |
Logical AND |
&& |
Returns true if both conditions are true and false if one or both conditions are false |
Logical OR |
|| |
Returns true if one or both conditions are true and false when both are false |
Logical NOT |
! |
Returns true if the condition is not satisfied else false |
Example: In the below example, the contract logicalOperator demonstrates the above mentioned different types of logical operators.
Solidity
pragma solidity ^0.5.0;
contract logicalOperator{
function Logic(
bool a, bool b) public view returns(
bool , bool , bool ){
bool and = a&&b;
bool or = a||b;
bool not = !a;
return (and, or, not);
}
}
|
Output :

Bitwise Operators
These operators work at a bit level used to perform bit-level operations. Solidity supports the following arithmetic operators :
Operator |
Denotation |
Description |
Bitwise AND |
& |
Performs boolean AND operation on each bit of integer argument |
BitWise OR |
| |
Performs boolean OR operation on each bit of integer argument |
Bitwise XOR |
^ |
Performs boolean exclusive OR operation on each bit of integer argument |
Bitwise Not |
~ |
Performs boolean NOT operation on each bit of integer argument |
Left Shift |
<< |
Moves all bits of the first operand to the left by the number of places specified by the second operand |
Right Shift |
>> |
Moves all bits of the first operand to the right by the number of places specified by the second operand |
Example: In the below example, the contract SolidityTest demonstrates the above mentioned different types of bitwise operators.
Solidity
pragma solidity ^0.5.0;
contract SolidityTest {
uint16 public a = 20;
uint16 public b = 10;
uint16 public and = a & b;
uint16 public or = a | b;
uint16 public xor = a ^ b;
uint16 public leftshift = a << b;
uint16 public rightshift = a >> b;
uint16 public not = ~a ;
}
|
Output :

Assignment Operator
These operators are for the assignment of value to a variable. The operand at the left side is variable while operand at the right side is value. Solidity supports the following arithmetic operators :
Operator |
Denotation |
Description |
Simple Assignment |
= |
Simply assigns the value at the right side to the operand at the left side |
Add Assignment |
+= |
Adds operand at the right side to operand at the left side and assigns the value to left operand |
Subtract Assignment |
-= |
Subtracts operand at the right side from operand at the left side and assigns the value to left operand |
Multiply Assignment |
*= |
Multiplies both the operands and assign the value to left operand |
Divide Assignment |
/= |
Divides operand at the left side by operand at the right side and assign the value to left operand |
Modulus Assignment |
%= |
Divides operand at the left side by operand at the right side and assign the remainder to left operand |
Example: In the below example, the contract SolidityTest demonstrates the above mentioned different types of assignment operators.
Solidity
pragma solidity ^0.5.0;
contract SolidityTest {
uint16 public assignment = 20;
uint public assignment_add = 50;
uint public assign_sub = 50;
uint public assign_mul = 10;
uint public assign_div = 50;
uint public assign_mod = 32;
function getResult() public {
assignment_add += 10;
assign_sub -= 20;
assign_mul *= 10;
assign_div /= 10;
assign_mod %= 20;
return ;
}
}
|
Output :

Conditional Operators
It is a ternary operator that evaluates the expression first then checks the condition for return values corresponding to true or false.
Syntax:
if condition true ? then A: else B
Example: In the below example, the contract SolidityTest demonstrates the conditional operator.
Solidity
pragma solidity ^0.5.0;
contract SolidityTest{
function sub(
uint a, uint b) public view returns(
uint){
uint result = (a > b? a-b : b-a);
return result;
}
}
|
Output :

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!