Quick Guide To Solidity Operators
In this post, we will closely examine Solidity operators and various types of operators supported by Solidity, providing examples and code snippets to help you understand how to use them in your own smart contracts.
Whether you are a beginner or an experienced developer, this guide will help you gain a deeper understanding of Solidity operators.
Solidity Operators
To create effective smart contracts, it is important to have a solid understanding of the Solidity operators.
Solidity supports a wide range of operators, including arithmetic, bitwise, logical, comparison, and assignment operators, among others.
By mastering these operators, You can write cleaner, more concise, and more efficient code.
What is an Operator?
Let’s look at a simple expression: 7+ 6 = 13. In this case, 7 and 6 are known as operands, while + is the operator.
Here is a list of the types of operators supported by Solidity.
- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators
Let’s examine each operator one by one.
Solidity Operator Arithmetic
Arithmetic operators supported by Solidity include the following:
In the case of variable mrx holding 7 and variable ample holding 5, then:
Sr.No. | Operators & Overview |
1 | + (Addition)
The sum of two operands (mrx and ample) will be: Ex: mrx + ample will give 12 |
2 | – (Subtraction)
Finds the difference of ample from mrx Ex: mrx – ample will give 2 |
3 | * (Multiplication)
Displays the product of mrx and ample Ex: mrx * ample will give 35 |
4 | / (Division)
Divide the mrx by the ample Ex: mrx / ample will give 1 |
5 | % (Modulus)
Displays the remainder of an integer division Ex: mrx % ample will give 2 |
6 | ++ (Increment)
Make an increment of one to the integers value Ex: mrx++ will give 8 |
7 | — (Decrement)
Decreases an integer value by one Ex: ample– will give 4 |
Example:
Solidity Operator Comparison
The comparison operators are used to compare two variables.
Considering the scenario we took in the above code let’s explore the working of comparison operator :
Correspondingly when variable mrx=7 and ample=5 then:
Sr.No. | Overview |
1 | = = (Equal)
In this method, the value of two operands is checked against each other to see if they are equal; if they are, the condition becomes true else otherwise. Ex: (mrx == ample) is not true. |
2 | != (Not Equal)
A condition is tested to see if the value of two operands are equal or not. If the operands do not contain the same value, the condition is true. Ex: (mrx != ample) is true. |
3 | > (Greater than)
This function checks whether the left operand value is greater than the right operand value, if it is, then the condition becomes true, otherwise it will be false. Ex: (mrx > ample) results as true. |
4 | < (Less than)
It checks whether the left operand value is greater than the right operand value, and if it is, the condition becomes true. Otherwise, it returns false. Ex: (mrx < ample) is false. |
5 | >= (Greater than or Equal to)
It checks if the left operand has higher or equal value to the right operand; if it does, then it becomes true. Otherwise, the condition remains false Ex: (mrx >= ample) is true. |
6 | <= (Less than or Equal to)
This function checks whether the value of the left operand is less than or equal to the value of the right operand, if the answer is yes, then the condition becomes true, otherwise it becomes false. Ex: (mrx <= ample) is false. |
Example:
Solidity Operator Logical
The meaning of a logical operator or operator phrase is a symbol or word that is used to connect two expressions.
This means that the value of the compound expression generated only depends on the value of the original expression as well as the meaning of the operator or phrase.
There are a number of logical operators that are commonly used, including AND, OR, and NOT.
Here mrx=7 and ample=5 as we used in the codes above:
Sr.No. | Overview |
1 | && (Logical AND)
If both conditions are true, the output comes as true: Ex: (mrx>2 && ample>3) is true. |
2 | || (Logical OR)
If any of the one condition is true, it returns true: Ex: (mrx>4 || ample<3 ) is true. |
3 | ! (Logical NOT)
It reverses the result of the logical operators, if result is true then it shows fale and vise versa., Ex: ! (mrx>4 && ample==5) is false. |
Assuming the Scenario we used in the codes above, here we can understand the working of the Logical Operators:
Example:
Solidity Operator Bitwise
The bitwise AND operator ( & ) is used to compare each bit of the first operand with the corresponding bit of the second operand.
In the case of two bits that are 1, the resulting bit is set to 1. Otherwise, the bit corresponding to the result is set to 0. In order to perform a bitwise AND, both operands must be integral types.
Let’s take a variable, name it as mrx and assign it with a value 4, and declare another variable ample and assign it with a value 2:
Sr.No. | Overview |
1 | & (Bitwise AND)
A Boolean AND operation is performed on each bit of its integer arguments. Ex: (mrx & ample) is 0. |
2 | | (BitWise OR)
The Boolean OR operation is applied to each bit of the integer arguments. Ex: (mrx | ample) is 6. |
3 | ^ (Bitwise XOR)
This function performs an exclusive Boolean OR operation on each bit of its integer arguments. In an exclusive OR situation, either operand one or operand two will be true, but neither will be true at the same time. Ex: (mrx ^ ample) is 6. |
4 | ~ (Bitwise Not)
This is a unary operator that reverses all the bits in the operand when it is applied. Ex: (~mrx) is 0. |
5 | << (Left Shift)
First operand’s bits are moved to the left by the number of places specified in the second operand. Whenever a new bit is created, it is filled with zeros. When you shift a value left by one position, it is equivalent to multiplying it by 2. If you shift it two positions, it is equivalent to multiplying it by 4. Ex: (mrx << 2) is 16. |
6 | >> (Right Shift)
By using the Binary Right Shift Operator, the left operand’s value is shifted right by the right operand’s number of bits. Ex: (mrx >> 2) is 16. |
7 | >>> (Right shift with Zero)
There is a very similar operator, the >> operator, but the bits shifted in on the left are always zero. Ex: (mrx >>> 1) is 5. |
Example:
Sr.No. | Overview |
1 | = (Simple Assignment )
A value is assigned to the left operand from the right operand Ex: mrx= ample , in this case the value of ample is assigned to the varaible mrx |
2 | += (Add and Assignment)
Right operand is added to left operand, then result is assigned to left operand. Ex: mrx += 3 is equivalent to mrx = mrx + 3 |
3 | –= (Subtract and Assignment)
This function subtract the right operand from the left operand and assigns the result to the left operand. Ex: ample -= 5 is equivalent to ample = ample – 5 |
4 | *= (Multiply and Assignment)
The result of multiplying the right operand with the left operand is assigned to the left operand. Ex: mrx*= ample is equivalent to mrx = mrx * ample |
5 | /= (Divide and Assignment)
Left operand and right operand are divided together, and the result is assigned to the left operand. Ex: mrx/= ample is equivalent to mrx = mrx / ample |
6 | %= (Modules and Assignment)
Modulus is calculated with two operands and assigned to the left operand. Ex: ample %= 4 is equivalent to ample = ample % 4 |
Example:
Solidity Conditional Operator (?
Conditional operators evaluate expressions to determine whether they are true or false. Then, based on the result of the evaluation, they execute either of the two given statements.
Sr.No. | Overview |
1 | ? : (Conditional )
The value will be X if the condition is true, otherwise it will be Y if the condition is false |