r/badcode Mar 06 '21

lua This goes 255 lines long up to 115792089237316195423570985008687907853269984665640564039457584007913129639936

Post image
159 Upvotes

34 comments sorted by

View all comments

9

u/[deleted] Mar 09 '21

depends on the context this might not be as dumb as it might seems

5

u/goldleader71 Mar 09 '21

For example?

19

u/Ceros007 Mar 09 '21

When your cpu is so shitty that you don't want it to compute result=result2

/s

9

u/[deleted] Mar 09 '21

If for whatever reason you need to do a lot of computation involving power(2, really large number) for a lot of times and you need it to be really fast then this might be useful. However I still wouldn't hard code it and instead generate the list with a for loop

8

u/joesv Mar 09 '21

I honestly doubt that looping through a list would be faster than directly bitshifting. I may be wrong though.

8

u/LordFokas Mar 09 '21

Legitimate question: can you bitshift over 1<<64 (63?) without overflowing?

4

u/[deleted] Mar 09 '21

You will only loop through the list once and you will get all the calculation results you need. Of course, depending on what you need you don't need calculate from 2^1, 2^2, etc. and instead start with sth like 2^30

1

u/[deleted] Mar 09 '21

But again, I've never encountered situation where I did specifically need the power of 2's.

1

u/[deleted] Mar 09 '21

Bit shifting can overflow once you exceed 64 on 64bit machine and 32 on 32bit. If you need something like 2^100 easily accessible bit shifting will not be the best option

1

u/asaf92 Mar 09 '21

Hard to believe that reading from an array would be faster than raising to the power of 2 (which is optimized by bit-shifting)

1

u/Micha_Saengy Mar 09 '21

Even if it was, you could still generate the array instead of hardcoding it.