1

I got this question in an interview and I am really not sure if I am correct.

Numbers are encoded in hex format and they are big numbers. But what is the reason behind that? I tried to google a lot but couldn't really get to the satisfactory answer.

2 Answers 2

3
+50

It's because computers nowadays are running either on 32 bit or 64 bits processors. This is their "word" size. (32 is actually a double word and 64 a Quad word but you get the idea)

The largest integer representation truly natively supported by those processors is actually the size of a "word" according to their architecture. This is all fine as long as you stay within those bounds, but with the 256 bits EVM words you are way past that...

One example I like to use, 32 bits architecture were limited to 4GB of addressable RAM (before address expansion "hacks"), turns out that 4GB is 2^32. You cannot go above because you don't have enough bits to represent a higher integer number natively...

So to accommodate every architecture and keep the EVM running on 256 bits words , we must rely on non-native types that do require more operations but are able to handle size up to 256 bits (Big numbers can go way beyond that). Plus, it provides a uniform number format that is agnostic of the underlying architecture.

You may find that we can natively represent such numbers with floating point representation, but they do come with some precision issues that is not desirable in financial / precision systems.

2
  • Hi, I see that data I fetch is bignumber and is comverted using toString method. Feb 3, 2022 at 8:17
  • Yes, that's perfectly fine for display. The BigNumber format allows you to have access to almost every possible arithmetic operations too if required.
    – hroussille
    Feb 3, 2022 at 9:42
2

The reason behind this is that numeric values presented in blockchain (e.g. Ethereum) can be really big.

Consider uint256, it means it can hold a value as big as 2**256, which when calculated, doesn't really have a human-readable name, neither will it have. Blockchain can store this data, as it is designed from the start for that, but frameworks and languages that interact with it can't. For example, the maximum value that Javascript can safely store is as big as 2**53. If you try to present maximum numeric value in Blockchain via Javascript number, you can see it's not possible.

Hexadecimal values are therefore used to present big numbers, as this is a convenient way to store big numeric values, and that's why they are utilized in this use case. There are a couple of Javascript libs for Big Number manipulation, but in the nutshell, they all work more or less the same and they all provide a way to convert hexadecimal value to string or numeric value (if it can be presented as such, in case it's too big to be stored).

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.