Arithmetic Operators
Introduction
Arithmetic operators are used in Solidity to perform mathematical operations on numeric data types. These operations include addition, subtraction, multiplication, and division, as well as modulo, exponentiation, and bitwise operations. Solidity 0.8.16 supports all of these operations, and in this blog post, we'll explore each one in detail.
Addition
The addition operator +
is used to add two numeric values together. For example, x + y
would add the values of x
and y
.
Here's an example of using the addition operator in Solidity:
pragma solidity 0.8.16;
contract MyContract {
uint x = 5;
uint y = 10;
uint result = x + y; // result is 15
}
Subtraction
The subtraction operator -
is used to subtract one numeric value from another. For example, x - y
would subtract the value of y
from x
.
Here's an example of using the subtraction operator in Solidity:
pragma solidity 0.8.16;
contract MyContract {
uint x = 10;
uint y = 5;
uint result = x - y; // result is 5
}
Multiplication
The multiplication operator *
is used to multiply two numeric values together. For example, x * y
would multiply the values of x
and y
.
Here's an example of using the multiplication operator in Solidity:
pragma solidity 0.8.16;
contract MyContract {
uint x = 5;
uint y = 10;
uint result = x * y; // result is 50
}
Division
The division operator /
is used to divide one numeric value by another. For example, x / y
would divide the value of x
by y
.
Note that division in Solidity is integer division, which means that the result will be rounded down to the nearest integer. To perform floating-point division, you can use fixed-point arithmetic.
Here's an example of using the division operator in Solidity:
pragma solidity 0.8.16;
contract MyContract {
uint x = 10;
uint y = 5;
uint result = x / y; // result is 2
}
Modulo
The modulo operator %
is used to find the remainder of one numeric value divided by another. For example, x % y
would find the remainder when x
is divided by y
.
Here's an example of using the modulo operator in Solidity:
pragma solidity 0.8.0;
contract ModuloExample {
function findRemainder(uint256 x, uint256 y) public pure returns (uint256) {
return x % y;
}
}
In this example, we define a function called findRemainder
that takes two unsigned integer parameters, x
and y
, and returns their remainder using the modulo operator.
Let's say we call the findRemainder
function with x = 17 and y = 5. The result will be 2, because 17 divided by 5 is 3 with a remainder of 2.
Conclusion
In this article, we have covered the arithmetic operators available in Solidity, including addition, subtraction, multiplication, division, modulo. We have seen how these operators can be used to perform mathematical and logical operations on unsigned integers in Solidity contracts.
Starting with Solidity 0.8 integer and unsigned integer overflow and underflow, is automatically checked. Previously, libraries such as OpenZeppelin's SafeMath
library was used for overflow and underflow manually in the code.
Overall, understanding the behavior and usage of arithmetic operators in Solidity is an essential aspect of writing secure and effective smart contracts on the Ethereum blockchain.
Feedback
Have feedback? Found something that needs to be updated, accurate, or explained?
Join our discord and share what can be improved.
Any and all feedback is welcomed.