11

I am compiling using sol 0.8.0 and the following line is producing the error in the title:

uint256 public constant MASK = uint128(~0);

Error:

Explicit type conversion not allowed from "int_const -1" to "uint128".

When changing the variable to this:

uint256 public constant MASK = ~0;

The error changes to:

Type int_const -1 is not implicitly convertible to expected type uint256. Cannot implicitly convert signed literal to unsigned type.

2 Answers 2

10

Changed it to this:

uint256 public constant MASK = type(uint128).max;

New restrictions in 0.8.0

4

From solidity >0.8.0.

Explicit conversions between literals and an integer type T are only allowed if the literal lies between type(T).min and type(T).max. In particular, replace usages of uint(-1) with type(uint).max.

1
  • 3
    OP reefers to type(uint128).max, while type(uint).max is shortcut for type(uint256).max Commented Nov 28, 2021 at 22:25

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.