DEV Community

Shlok Kumar
Shlok Kumar

Posted on

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:

uint8 = 5;
uint16 = 100;
uint24 = 1000;
uint32 = 104833;
uint40 = 18348;
uint56 = 324234;
uint64 = 134243;
uint128 = 11334;
uint136 = 24;
uint256 = 100000;
Enter fullscreen mode Exit fullscreen mode

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:

int8 = 5;
int16 = -100;
int24 = -1000;
int32 = 104833;
int40 = -18348;
int56 = -324234;
int64 = 134243;
int128 = -11334;
int136 = 24;
int256 = -100000;
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)