![]() |
VOOZH | about |
Integers help store numbers in smart contracts. Solidity provides two types of integers:
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.
Solidity supports arithmetic, logical, and bitwise operations with signed integers. Signed integers represent balances, prices, and other positive or negative quantities in smart contracts. Signed integers may overflow and underflow, which can affect smart contracts. Int variables wrap around to their minimum values if increased past their maximum values. Handling signed integers carefully helps prevent smart contract problems and vulnerabilities.
Signed integers in Solidity represent positive and negative whole numbers. In two's complement notation, the MSB represents the sign while the remaining bits denote the magnitude. Solidity's int types may represent numbers from -2^255 to 2^255-1 and have a default size of 256 bits. Avoid overflow and underflow when using signed integers in smart contracts' arithmetic, logical, and bitwise operations.
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.
Like signed integers, Solidity unsigned integers are used for arithmetic, logical, and bitwise operations. In smart contracts, they reflect token balances and cryptocurrency supply. As they lack a sign bit, unsigned integers are less likely to overflow or underflow. Nonetheless, contracts should not exceed the maximum value of an unsigned integer since this might produce unexpected behavior.
Solidity unsigned integers represent only positive entire values. Signed integers are more prone to overflow and underflow than unsigned numbers. Solidity has fixed-size uint types with a default size of 256 bits for values from 0 to 2^256-1. Smart contracts employ unsigned integers to represent token balances and cryptocurrency supply for arithmetic, logical, and bitwise operations.
Below are the differences between signed integers and unsigned integers:
| Parameters | Signed Integers | Unsigned Integers |
|---|---|---|
| Represents | Positive and negative whole numbers. | Non-negative whole numbers. |
| Data Type | int | uint |
| Default Size | 256 bits | 256 bits |
| Size Range | 8-256 bits | 8-256 bits |
| Positive Values Representation | The most significant bit (MSB) is 0 | All bits represent the magnitude |
| Negative Values Representation | The most significant bit (MSB) is 1 | N/A |
| Maximum Value | 2^(n-1)-1 | 2^n-1 |
| Overflow and Underflow Issues | More prone to overflow and underflow issues due to negative values representation | Less prone to overflow and underflow issues |
| Common Use Cases | Balances, prices, and other numbers that may be positive or negative | Quantities, amounts, and other values that are guaranteed to be non-negative |
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:
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.
Output: