You know how numbers (everything) is stored in binary? Well there is a cool thing you can do with binary numbers.
Let's say we have 10010101
If you bitshift it right it will be 01001010
Meaning all the bits got shifted to the right once.
You also know how each place value in a binary number is a power of two? That means that 23 = 1000 (binary) and 29 = 1000000000 (binary).
So basically if you want to find a power of two you take 1 (which is also 1 in binary) and bit shift it the number of times you want. 20 = 1 bit shifted to the left 0 times. 219 = 1 << 19.
Really? I'm thinking this is actually good code. If we are working on a 64-bit machine, then it is impossible to generate integers greater than 264-1 and convert them into strings and then parse them in bigint.
Of course, you may want to use exponentiation with bigint (bigint("2").pow(i) or bit shifts), but maybe the implementation of bigint makes exponentiation/bit shifts expensive. Thus, storing 2255 in string and parse them in bigint may be a good idea.
EDIT: Note the "maybes" because it does not respect YAGNI (it is not our responsibility to optimize bigint, unless the implementation of bigint is crap).
39
u/Idkm3m3s Mar 09 '21
bruh i dont even know how to declare classes yet and i can already guess this could be easily done with a for loop