# 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 mrxEx: mrx – ample will give 2 3 * (Multiplication)Displays the product of mrx and ampleEx: mrx * ample will give 35 4 / (Division)Divide the mrx by the ampleEx: mrx / ample will give 1 5 % (Modulus)Displays the remainder of an integer divisionEx: mrx % ample will give 2 6 ++ (Increment)Make an increment of one to the integers valueEx: mrx++ will give 8 7 — (Decrement)Decreases an integer value by oneEx: ample– will give 4

#### Example:

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract SolidityTest{uint public mrx=7; uint public ample=5;function Add_Integers() public view returns(uint){uint sum=mrx+ample; return sum; }function Subtract_Integers() public view returns(uint){uint difference=mrx-ample; return difference; }function Multiply_Integers() public view returns(uint){uint product=mrx*ample; return product; }function Divide_Integers() public view returns(uint){uint devide=mrx/ample; return devide; }function Modulus_Of_Integers() public view returns(uint){uint modulus=mrx%ample; return modulus; }function Increament_mrx() public returns(uint){mrx++; return mrx;}function Decrement_mrx() public returns(uint){mrx--; return mrx;}}

## 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 falseEx: (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:

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract SolidityTest{uint public mrx=7; uint public ample=5;function Equal_Condition() public view returns(bool){return mrx==ample; }function Not_Equal_Condition() public view returns(bool){return mrx!=ample; }function GreaterThan_Condition() public view returns(bool){return mrx>ample; }function LessThan_Condition() public view returns(bool){ return mrx<ample; }function GreaterThanEqualsTo_Condition() public view returns(bool){return mrx>=ample; }function LessThanEqualsTo_Condition() public view returns(bool){ return mrx<=ample;} }

## 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:

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract SolidityTest{uint public mrx=7; uint public ample=5;function AND_Operator() public view returns(bool){return (mrx>2 && ample>2); }function OR_Operator() public view returns(bool){return (mrx>4 || ample<2); }function Not_Operator() public view returns(bool){return !(mrx>4 && ample==5); }}

## 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:

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract SolidityTest{uint public mrx=4; uint public ample=2;function Bitwise_AND_Operator() public view returns(uint){return (mrx & ample); }function Bitwise_OR_Operator() public view returns(uint){return (mrx | ample); }function Bitwise_XOR_Operator() public view returns(uint){return (mrx ^ ample); }function Bitwise_Not_Operator() public view returns(uint){return (~mrx); }function Left_Shift_Operator() public view returns(uint){return (mrx << 2); }function Right_Shift_Operator() public view returns(uint){return (mrx << 2); }}

 Sr.No. Overview 1 = (Simple Assignment )A value is assigned to the left operand from the right operandEx: 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:

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract SolidityTest{uint public mrx=14; uint public ample=8;function Simple_Assignment_Operator() public returns(uint){mrx=10; // Here we modified the value of mrx return mrx; }function Add_and_Assign_Operator() public returns(uint){return (mrx+=3); // Value of mrx is added to three digits adn the corresponding value is assigned to the left operand }function Subtract_and_Assign_Operator() public returns(uint){return (ample-=5); }function Multiply_and_Assign_Operator() public returns(uint){return (mrx*=ample); }function Divide_and_Assign_Operator() public returns(uint){return (mrx /= ample); }function Modulus_Operator() public view returns(uint){return (ample % 4);} }
Bitwise operators will have the same logic as <<=, >>=, >>=, &=, |= and ^=

## 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

#### Example:

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract SolidityTest{uint public mrx=14; uint public ample=8;function Conditional_Operators() public view returns(uint){uint result=(mrx>ample ? mrx-ample : ample-mrx); return result;}}
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0