Ethernaut-Token — What if our alphabet could have only two letters?

Eszymi
Coinmonks
6 min readAug 24, 2022

--

Imagine a world where everyone knows only two letters, no less, no more. Everyone words which they use is created from this to letters. Additionally, let’s assume these letters are also numbers and in this world there aren’t more number than these two. Is it sound ridiculous? But what if I say to you, that kind of world exists, and we meet with it every day?

This world is world of digital electronic, world of our computers, smartphones and so. Logic used in this electronic is known as a Boolean algebra, and it knows only two numbers : 0 and 1. These numbers are sometimes called false and true. And these two number is used in every used by us devices. Every information we saved is convert to 0 and 1, but not just to one number but sequence of these two.

Let's imagine a line of 8 chairs. On every chair could sit 0 or 1, but not both of them in the same time. We are able to calculate how many different way they can sit. On every chair could sit one number from 2, so we have two possibility. And the same is for other chairs. Because we have 8 chairs, we have to multiply 2 with 2 eight times, so as a result we get 2⁸=256 combinations. So we see that using two numbers and set a sequence of 8 of them, we could have many different states. In electronic and informatic, we call one chair described here as a bit, and 8 of bits as a byte. A byte is a digital word. We can connect bytes to longer word and have bigger and bigger combination. And is necessary if you think how variety and difference kind information we use every day — music, films, photos, games etc. They all are saved as a sequence of bits. And it is stunning, isn’t it?

In solidity, we can find a type called bytes. They are provided a wide range — from bytes1 to bytes32. One byte, how we said, is a sequence of 8 bits. 32 bytes is sequences of 32 bytes, so it is a sequence of 32*8 = 256 bits. How we know sequence of 8 bits give us 2⁸ combination, so sequence of 256 bits is equal to 2²⁵⁶. It’s a very big number. But in solidity we could find also a type like uint256 and so on. What is this?

Before I give you an answer for that, I would like to you imagine yourself something strange — every of your hands has only one finger. And you want to count on the finger how many cars you see on the street. When you notice the first car, you straighten your first finger. With the next car, you have to hide straighten finger, and straight finger in the second hand. On the picture, we see these hands (sorry for very bad quality of picture, but drawing is not my the strongest ability :D ).

Counting on the hands with one finger how many cars was noticed

When we focus on just one hand, we could see the finger is hidden or not. So it has only two states, like value of bit. Adding second hand, we have in a sum 4 possibilities. Great, now we know if we add one hand more, we will have 8 possibility. But, except that we discover something grate. Math with 2 in base. What I mean by that.

Our daily math contains 10 digits: 0,1,2,3,4,5,6,7,8 and 9. Every number is created using these digits. If the number is bigger than 9 we create it by setting these digits in sequence, right? For example 42. But how could we write this number? 42 = 4 * 10 + 2. Ok, but how show number 4242? It is, 4242 = 4*10³ + 2*10² + 4*10 + 2 = 4*10³ + 2*10² + 4*10¹ + 2*10⁰. So if we will have a number long for n digits, we will write is like a

Number long for n digits. a, b, c, e and f are the digits from 0 to 9.

Noticed that every time we multiply value in sequence with 10 (number of digits) power to position of this value counting from the last and starting with zero.

Let’s put this pattern to world where is only 2 digits (binary). So every number bigger than 1 is by setting these digits in sequence. So number 1001 we could write as a 1*2³ + 0*2² + 0*2¹ + 1*2⁰. If we sum up it, we will see that 1001 in two digits math is equal to 9 in 10 digits math.

Knowing that, we could back to our talk about difference between uint256 and bytes256. In uint256 the number is represented in 10 digits math no in combination of bits like in bytes256. But unit256 still is saved in computer like a sequence of bits (more precisely 256 bits), therefor we have a limit of combinations. 256 bits give as 2²⁵⁶ = 1.1579209*10⁷⁷ combinations. So our math mustn’t be beyond this number if we use this type. So assume. uint256 and bytes32 have the same volume, but in the first number is represented in the 10 digits math. Bytes32 is a sequence of 0 and 1.

If we're now talking about digits, I would like to show differences between uint and int types. Now we know every number is saved in computer’s memory as a sequence of bits. But how we could represent minus number in these terms? The solution is simply. We have to assume that the first bit represent sign. And for example, if the first bit is 1 the number is negative, if 0 it is positive. But when we spend one bit to carry information about sign, the biggest number we could save changed. Let’s take bytes1 type. The maximal number is 2⁸-1=255 (we subtracted 1 because we have 2⁸ combination, and we start counting from 0) and the minimal is 0. When one bit will represent minus, the maximal number will be 2⁷ -1= 127, and the minimal
-1*2⁷+1 = -127. So as a result, adding information gave from us only one combination (-0 = 0), but change the maximal value. Therefor, we have to type uint (unsigned int) and int. The first doesn’t have information about sign, and it is always not negative.

Why information about number of bits is so important? The problem is with for example with adding

Similar of adding in math contains 10 digits and 2 digits

When we're adding one number to the other, the result sometimes is longer than the added numbers. In normal life it is no a problem. But what if we save only a limited number of digits? How we see in example of 2 digits, adding 1 made that in our 8 bits we have only zeros, so our number is zero. It called overflow. We have similar situation during the subtracting. Then it is underflow.

OpenZeppelin provides security products to build, automate, and operate decentralized applications

Overflow and underflow could be very dangerous, therefor when we work with numbers in Solidity we should use Math library from OpenZeppelin which protect us from this. Also, from 0.8 Solidity version, overflow and underflow will cause a revert.

I know what you can think now: Could it be other amount of digit than 2 or 10? And yes, it could be. If you want, it could be an arbitrary amount of them and the algorithm of using them is the same as in present cases. The most used amount except this two is 16 digits (hexadecimals). To represent 10, 11, 12, 13, 14 and 15 in just one sign we use consequently A, B, C, D, E and F. And the number AB = 10*16¹+11*16⁰ = 171. Hexadecimals are used popular to represent binary value in shorter form. Because one digit of hexadecimal could represent a value from 0 to 15 it is equal of 4 bits. Hexadecimal is popular to write the address for example.

I hope you find this post useful. If you have any idea, how could I make my posts better, let my know. I am always ready to learn. You can connect with me on LinkedIn and Telegram.

If you would like to talk with me about this or any other topic I wrote, feel free. I’m open to conversation.

Happy learning!

New to trading? Try crypto trading bots or copy trading

--

--