I've seen a contract that stores -1 as an unsigned integer.

uint256 can = allowed ? uint256(-1) : 0;

Can anyone explain the benefit of this over something like:

uint256 can = allowed ? 1 : 0;

4 Answers 4


By 2s-complement, uint256(-1) is equal to the maximum value of uint256.

So it's essentially a shorter way to write 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.

For bool can, your wondering would be correct (i.e., allowed ? true : false would suffice).

But since uint256 can is used, the answer to your question depends on what it is used for...

  • OK, that's clarified a lot. Digging further, this variable is used to set the spending allowance. I was looking at it as if it were setting a boolean in some way.
    – Nick
    Commented Feb 25, 2020 at 14:07
  • 1
    @Nick: Yeah, if it was setting a boolean, then most likely no code was needed to begin with, since that boolean is already given as input, right? I mean, what else could allowed be? But you're right about the name can implying of a boolean variable; it's probably just a bad naming-choice. Commented Feb 25, 2020 at 14:15

uint256(-1) is a trick to gives the maximum uint256 value.

  • Note that it was already mentioned a few minutes ago in the answer above. Commented Feb 25, 2020 at 14:06

Use type(uint256).max for better readability. Supports Solidity >= 0.6.8


Here is another REAL purpose

Tests are passing:

for (uint256 i = pendingDepositsLength - 1; i != uint256(-1); i--) {

Test are failing:

for (uint256 i = pendingDepositsLength - 1; i >= 0; i--) {


i-- causes underflow and i becomes a really large number.

uint256(-1) solves that. Clever use case, not sure about readability.

I wanted to refactor but the failing tests raised the alarm.

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.