The uint and int data types in Solidity are used to store integer values. The uint data type can store unsigned integers, with values of zero or greater than zero but not negative numbers. To store negative numbers the int data type is used.
uint public u = 123; // unsigned integer
int public i = -123; // integer
different sizes of uint and int
uint is an alias of uint256 and can store values between 0 to 2**256 -1
To store different sizes of integers the uint data type allows to define the size in steps of 8:
uint8 stores values between 0 to 2**8 -1
uint16 stores values between 0 to 2**16 -1
uint 24 stores values between 0 to 2**24 -1
uint32 stores values between 0 to 2**32 -1
...
uint 256 stores values between 0 to 2**256 -1
int is an alias of int256 and can store values between -2**255 and 2**255 -1
int allows to store negative numbers and offers different sizes in steps of 8 like the uint type.
using uint8 or int8 for example is not always cheaper in gas costs than using uint256 or int256. The reason is the 32 bytes architecture of the Ethereum Virtual Machine (EVM) and the gas costs of the conversion. We will discuss more on this later when we talk about gas optimization.
max and min values
To find the maximum and minimum values to be stored in the int data type we can use type(int).min and type(int).max
int public min_int = type(int).min; // minimum value of int data type
int public max_int = type(int).max; // maximum value of int data type
uint public min_uint = type(uint).min; // minimum value of uint data type
uint public max_uint = type(uint).max; // maximum value of uint data type
As we can see the minimum value of uint is 0.
operators
uint and int data types work with the following operators.
operators |
examples |
comparison operators (evaluate to boolean value) |
<=, <, ==, !=, >=, > |
bitwise operators |
&, |, ^, ~ |
shift operators (left and right shift) |
<<, >> |
arithmetic operators |
+, -, *, /, %, ** |