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?
1

1 Answer 1

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.

7
  • how can both types be stored in 256 bits, logically the int should require 2 times bigger storage space?:)
    – Eugene1111
    Commented Sep 9, 2023 at 18:12
  • 1
    also the answers seems to be the chatGpt response hahaah
    – Eugene1111
    Commented Sep 9, 2023 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. Commented Sep 9, 2023 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
    Commented Sep 10, 2023 at 13:58
  • Ye both answers are plagiarised GPT responses posted at the same time, should be removed.
    – Maka
    Commented Sep 10, 2023 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.