0

I am working on a Solidity smart contract and am considering the use of uint and int data types for memory variables. I have specific use-cases like reverse looping, where using int seems to produce cleaner code.

My questions are:

  1. Are there any performance differences between using uint and int in terms of computational cost and gas consumption?
  2. Are there best practices around the choice of these data types in Solidity, especially considering the web3 development space?
MShakeG
  • 1,603
  • 6
  • 37
Eugene1111
  • 69
  • 6
  • Related https://ethereum.stackexchange.com/questions/21200/how-does-solidity-the-evm-store-a-uint-vs-an-int – Ismael Sep 11 '23 at 02:43

1 Answers1

1

The main difference between uint and int is that uint only accepts positive values, while int accepts positive and negative values. They both store a 256-bit size value and as such, have the same storage cost.

The gas cost would vary depending on the operations you will make. Generally speaking gas cost may be higher for int if negative numbers are involved because handling negative numbers in smart contracts can be more complex.

To summarize, uint is often preferred for cases where you want to ensure that a variable is always non-negative, while for doing arithmetic operations, int is better.

Lejdi Prifti
  • 119
  • 4
  • how can both types be stored in 256 bits, logically the int should require 2 times bigger storage space?:) – Eugene1111 Sep 09 '23 at 18:12
  • 1
    also the answers seems to be the chatGpt response hahaah – Eugene1111 Sep 09 '23 at 18:14
  • No, it changes only with 1 bit to represent the sign, negative or positive which is not quite considerable as a cost. – Lejdi Prifti Sep 09 '23 at 21:25
  • A uint256 allows for the range [0, 2^256 - 1], in int256 1 bit is to indicate sign bit(+/-) and the remaining 255 bits represent the unsigned part(absolute value) which can give you a range of 2^255 values so for an int256 the negative range is [-2^255, -1] and the positive range is [0, 2^255 -1] – MShakeG Sep 10 '23 at 13:58
  • Ye both answers are plagiarised GPT responses posted at the same time, should be removed. – Maka Sep 10 '23 at 21:51
  • Please, do not use AI tools to answer questions. – Ismael Sep 11 '23 at 02:40
  • Take it to a plagarize service and check if it is done with ChatGPT. – Lejdi Prifti Sep 11 '23 at 04:51