uint vs uint256

Below are the differences between uint and uint256:

  Range Default size  Typical use cases
uint 0 to 2^256-1  256 bits  General-purpose unsigned integer type
uint8 0 to 2^8-1  8 bits  Small unsigned integers
uint16 0 to 2^16-1  16 bits  Representing values that require more bits than uint8
uint32 0 to 2^32-1  32 bits  Representing values that require more bits than uint16
uint64 0 to 2^64-1  64 bits  Representing values that require more bits than uint32
uint128 0 to 2^128-1  128 bits  Large unsigned integers
uint256 0 to 2^256-1  256 bits  Large unsigned integers

Solidity’s uint256 and uint are equal. Size and value range distinguish uint from other uint types. Using a smaller uint type may save gas and storage space for variables with a narrower range. If a variable only has to represent numbers from 0 to 255, using uint8 instead of uint256 saves gas and storage.

If the variable needs more values, uint256 is needed. Overflow and underflow issues occur when a smaller uint type represents a bigger number. Hence, the uint type depends on the use case and variable range.

Example:

Below is the Solidity program to implement integers:

Solidity




// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
contract IntegerExample {
   uint256 public myUint;
   int256 public myInt;
    
   function setValues(uint256 _myUint, int256 _myInt) public {
       myUint = _myUint;
       myInt = _myInt;
   }
   
   function multiply(uint256 _value) public view returns (uint256) {
       return myUint * _value;
   }
}


Explanation: In this example, we define a contract called IntegerExample that has two state variables, myUint and myInt, both of which are assigned the value 0 by default. We also define two functions, setValues and multiply.

  • The setValues function takes two input parameters, a uint256, and an int256, and sets the values of myUint and myInt to those input values, respectively. 
  • The multiply function takes a uint256 input parameter _value and returns the product of myUint and _value.

Output:

Remix Output



Solidity – Integers

Integers help store numbers in smart contracts. Solidity provides two types of integers:

  • Signed Integers: Signed integers can hold both positive and negative values, ranging from -2 ^ 255 to 2 ^255 – 1.
  • Unsigned Integers: Unsigned integers can hold only integer values equal to or greater than zero, ranging from 0 to 2 ^256 – 1.

Similar Reads

What is Signed Integer?

Signed integers represent positive and negative whole numbers. Signed integers are int in Solidity. In two’s complement notation, a signed integer’s MSB denotes its sign. Zero MSB is positive. MSB 1 is negative. The number magnitude is the leftover bits....

What is an Unsigned Integer?

Positive integers are unsigned. Solidity uses uint for unsigned numbers. Unsigned integers do not utilize the MSB to identify signs. Unsigned integers indicate the number magnitude with all bits....

Signed integer vs Unsigned integer

Below are the differences between signed integers and unsigned integers:...

uint vs uint256

Below are the differences between uint and uint256:...

Contact Us