Codementor Events

Solidity Data Types (int & uint)

Published Feb 08, 2022
Solidity Data Types (int & uint)

Understanding Solidity data types is important for developers who want to code smart contracts. In this post we will go over some of the solidity data types and their features.

Solidity is a typed language. A typed language is one in which the type of the variables is specified at compile time. Which means that they can only accept the data type they were originally intended for by the developer and can not be dynamically changed.

Integers

Solidity can have signed (a regular int) or unsigned (uint) integers. A regular int type means that the integer can either be positive or negative with a negative sign.
uint (unsigned integer) has a different range than the signed one.
For example, uint8 ranges from 0 to 255 while an int8 can hold from -128 to 127. So the items in the range is same, but the start and end integer in it itself are different. Another note, why is it from -128 to 127? because 0 is also an unsinged integer, so positive integers begin from 0 to 127 (128 total) and negative from -1 to -128 (again, 128 total), making the gross total to 256.

uint256 is same as uint in Solidity

In solidity, you can use the term uint256 and uint interchangably. This is not true for all languages. A recommended way is to use uint256 even when you can use uint only, becuase:

  1. It will allow you and other code readers to quickly remind themselves that it's a 256 uint, so that you can keep your code balanced accordingly
  2. You will not develop a wrong habit of using the same thing in other language and ending up getting some error or incorrect data handling

Same logic and range sequence applies to other int and uint like int16, int32, int 128, int256 etc.

Gas required for different data types

A quick note, the gas required for operations involving different data types or even between int8 or int256 are different. Also, they don't follow the simple logic of simpler data type consuming less gas, uint8 might cost more gas in an operation than uint256, so be careful when selecting them and dyor (do your own research) about it.

Discover and read more posts from nabtron
get started
post commentsBe the first to share your opinion
Show more replies