30

With regard to Solidity, What is UINT256?

From the token example at https://ethereum.org/token :

/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;

Beyond being a variable type in general computing, I'd love to gain a better of understanding and context in the Ethereum world. Why not just use an INT? Assuming it's a specific type of Integer, what does the "U" denote?

eth
  • 85,679
  • 53
  • 285
  • 406
Bruce Seymour
  • 438
  • 1
  • 4
  • 9
  • It is an integer type https://solidity.readthedocs.io/en/develop/types.html#integers – Ismael Nov 05 '17 at 16:21
  • 1
    I found, uint means unsigned int. uint doesn't allow for negative numbers, it has a range of 0 to 4,294,967,295, compared to the range of -2,147,483,648 to 2,147,483,647 for an int (Source: https://stackoverflow.com/questions/3068474/difference-of-using-int-and-uint-and-when-to-use) – Bruce Seymour Nov 05 '17 at 16:30
  • @BruceSeymour Those are default C# types - I'm pretty sure C# has a default int length that's way shorter than Solidity's default, thus different ranges. – Maximillian Laumeister Nov 04 '19 at 18:19

2 Answers2

33

With regard to Solidity...

This is really more a general computer science question that would best be answered on Stack Overflow.

At the risk of repeating what @Ismael has linked to...

  • U - unsigned (meaning this type can only represent positive integers, not positive and negative integers)
  • INT - integer
  • 256 - 256 bits in size

Context: The EVM (Ethereum Virtual Machine) uses 256 bits as its word size. See: Rationale behind 256-bit words in EVM

Richard Horrocks
  • 37,835
  • 13
  • 87
  • 144
  • 1
    Thanks, the context with regard to solidity is helpful. I also found this article: https://stackoverflow.com/questions/247873/signed-versus-unsigned-integers – Bruce Seymour Nov 05 '17 at 16:37
  • it's better to give an example of what a uint256 looks like. – Siwei Jan 12 '22 at 01:53
18

Integers in Solidity:

uint256 (uint is an alias) is a unsigned integer which has:

  • minimum value of 0
  • maximum value of 2^256-1 = 115792089237316195423570985008687907853269984665640564039457584007913129639935 //78 decimal digits

int256 (int is an alias) is a signed integer which has:

  • minimum value of -2^255 = -57896044618658097711785492504343953926634992332820282019728792003956564819968
  • maximum value of 2^255-1 = 57896044618658097711785492504343953926634992332820282019728792003956564819967

For example, in Solidity we could write the following code:

uint8 public constant decimals = 6;
uint256 public constant totalSupply = 1000000*10**uint256(decimals); // 1000000000000

P.S. It is unusual that int/uint in Solidity have 256 bits in size, because there are such popular languages as C#/Java that have int data type with 32 bits in size:

  • minimum value of -2^31 = -2147483648
  • maximum value of 2^31-1 = 2147483647
  • 3
    I think 256 bits was chosen so that there would be enough addresses for the ethereum network to continue to work indefinitely. – forgetso Jul 02 '19 at 08:14
  • 1
    @forgetso addresses in Ethereum are 160 bits, not 256 bits. Words in the EVM are 256 bits in length, and uint256 is almost always used for balances. – Luke Hutchison May 12 '22 at 04:38