4

I try to specify uint type to uint16 instead of just doing uint or uint256 when dealing with time stamps but it says that I must use at least uint24. It works with uint24, but shouldn't uint16 be plenty enough?

8

Not really an answer to what you're asking, but...

Decreasing the size of a uint to less that 256 bits will increase the associated gas costs, not decrease it like you might be expecting. It'll be best to leave it as a uint256.

See: Why does uint8 cost more gas than uint256?


Edit:

Noting that timestamps are the number of seconds since the Unix epoch:

  • uint16 maximum value = 65536 -> 01/01/1970 @ 6:12pm (UTC)
  • uint24 maximum value = 16777216 -> 07/14/1970 @ 4:20am (UTC)

So these values are too small.

Calling block.timestamp (or now) currently returns 1508618011, which is 0b1011001111010111010111100011011. This requires 31 bits. So you're looking at least at a uint32, but more if you want to be future-proof.

  • What if we were talking about something that would be saved to storage? I assume it would be best practices to forexample use uint8 instead of uint256 for decimals, saving gas costs too. – NowsyMe Oct 21 '17 at 20:17
  • Yep, true for variables written to storage - writing smaller quantities is cheaper. You'll want to experiment depending on how you're using the timestamps. – Richard Horrocks Oct 21 '17 at 20:31
  • Storage is written in blocks of 32 bytes. Using smaller values is not cheaper – Tjaden Hess Oct 21 '17 at 20:45
  • Okay, didn't know the block size - what Tjaden said :-) – Richard Horrocks Oct 21 '17 at 20:54
  • 2
    uint32 is more than enough, in my opinion: 2^32 - 1 equals to 4294967295, i.e. Sun Feb 07 2106. None of us will probably be alive at that time, and I highly doubt Solidity will still be used to write distributed applications :) – skozin Nov 23 '17 at 2:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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