[Ethereum] the purpose of uint256(-1)

solidity

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;

Best Answer

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...

Related Topic