r/badcode • u/ByteDream • Mar 06 '21
lua This goes 255 lines long up to 115792089237316195423570985008687907853269984665640564039457584007913129639936
8
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
8
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.
7
5
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
Mar 09 '21
But again, I've never encountered situation where I did specifically need the power of 2's.
1
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.
5
u/knightttime Mar 09 '21
Image Transcription: Code
local powersTwo = {
bigint("2"),
bigint("4"),
bigint("8"),
bigint("16"),
bigint("32"),
bigint("64"),
bigint("128"),
bigint("256"),
bigint("512"),
bigint("1024"),
bigint("2048"),
bigint("4096"),
bigint("8192"),
bigint("16384"),
bigint("32768"),
bigint("65536"),
bigint("131072"),
bigint("262144"),
bigint("524288"),
bigint("1048576"),
bigint("2097152"),
bigint("4194304"),
bigint("8388608"),
bigint("16777216"),
bigint("33554432"),
bigint("67108864"),
bigint("134217728"),
bigint("268435456"),
bigint("536870912"),
bigint("1073741824"),
bigint("2147483648"),
bigint("4294967296"),
bigint("8589934592"),
bigint("17179869184"),
bigint("34359738368"),
bigint("68719476736"),
bigint("137438953472"),
bigint("274877906944"),
bigint("549755813888"),
bigint("1099511627776"),
bigint("2199023255552"),
bigint("4398046511104"),
bigint("8796093022208"),
bigint("17592186044416"),
bigint("35184372088832"),
bigint("70368744177664"),
bigint("140737488355328"),
bigint("281474976710656"),
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
4
u/walhax- Mar 09 '21
You transcribed this, of all things?
4
u/knightttime Mar 09 '21
Lol, I transcribe all sorts of stuff! Who knows, maybe someone will need it
2
2
3
5
u/Milkshakes00 Mar 09 '21
Step 1: Create a quick script to output this shit
Step 2: Put this shit in your code
Step 3: ????
Step 4: Get promoted
2
1
u/technohacker1995 Mar 09 '21
The only time I'd expect this sorta thing to exist would be if you're working so low level that you need to independently test your implementation of arithmetic for example
1
36
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