0

I try to execute the following opcodes and computer gives me the following result

the code: type(uint).max * 2

the result: type(uint).max -1


PUSH1 2

PUSH32 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF


MUL

Could you explain this result?

1 Answer 1

2

The EVM has 256 bits operations, so the result of any operations that do not fit within the 256 will be truncate.

In this case the result should be:

0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe

When truncated to 256 it results in

0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
5
  • @EnginYILMAZ In that case 2 + 2**256 - 1 = 2**256 + 1, when truncated it results in 1. What do you mean by overflow? The EVM by itself doesn't check for overflow, it is a feature in some languages like vyper, or solidity after v0.8.
    – Ismael
    Jan 7 at 21:11
  • When we multiply max number with 2, result is type(uint).max * 2 = type(uint).max -1. however When we add max number with 1 , result is type(uint).max +1 =0 .... Jan 8 at 8:16
  • Clearly neither of you can answer a question or ask one correctly. This is basic math.
    – VX3
    Jan 8 at 14:44
  • 1
    @EnginYILMAZ I already answered that. It is the EVM truncating the result to fit the 256 bits.
    – Ismael
    Jan 8 at 21:33
  • @VX3 I didn't downvote your answer. The link was interesting but for me the answer wasn't clear enough. The site has people participating from many countries some of them don't use English on regular basis, so I always try to interpret the question beyond what they have written. We're all here to learn.
    – Ismael
    Jan 8 at 21:42

Your Answer

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

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