Integers in Solidity
Integer types in Solidity can be either unsigned or signed. Unsigned integers are referenced by the uint keyword. Unsigned integers can be assigned in steps of 8 bits up to 256 bits.
The following are the different types of unsigned integers:
|
The default value for uint type is uint256 and it can store 2^256 values, and because it is unsigned, the maximum value it can store is 2^256-1(zero requires one space). Signed integers are referenced by the int keyword. Signed integers can be assigned in steps of 8 bits up to 256 bits.
The following are the different types of signed integers:
|
The default value for uint type is int256 and it can store 2^256 values, and because it is signed , it contains positive as well as negative values centered around zero. The minimum and maximum values for int256 are -(2^256-1)/2 and (2^256-1)/2.
Integer Operators:
Comparisons: <=, <, ==, !=, >=, > (evaluate to bool)
Bit operators: &, |, ^ (bitwise exclusive or), ~ (bitwise negation)
Shift operators: << (left shift), >> (right shift)
Arithmetic operators: +, -, unary - (only for signed integers), , /, % (modulo), * (exponentiation)
For an integer type X, you can use type(X).min and type(X).max to access the minimum and maximum value representable by the type.
Comparisons:
The value of a comparison is the one obtained by comparing the integer value. This means, for example ~int256(0) == int256(-1).
For more content, follow me on - https://linktr.ee/shlokkumar2303