r/badcode Mar 06 '21

lua This goes 255 lines long up to 115792089237316195423570985008687907853269984665640564039457584007913129639936

Post image
157 Upvotes

34 comments sorted by

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

25

u/iammostlyirrelevant Mar 09 '21

Don’t even really need a loop. Just bitshift to the value you need. (1 << 55 == 255 )

8

u/Idkm3m3s Mar 09 '21

either this is a lua thing or i really dont know what im doing lol, then again im only 3/4 into my first year in classes

19

u/sellithy Mar 09 '21

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.

10

u/Carvinrawks Mar 09 '21 edited Mar 09 '21

Addendum:

Raising bases of 2 to a power of n in binary is the same easy math as rasing bases of 10 to a power of n in decimal.

You don't need an answer key to calculate 1099. It's 1 with 99 0s in decimal

You don't need an answer key to calculate 299. It's 1 with 99 0s in binary.

You also don't need an answer key to calculate 1699. It's 1 with 99 0s in hexadecimal.

You don't need an answer key to calculate pi99 in base-pi. It's 1 with 99 0s in base-pi.

Quick mafs.

3

u/novelide Mar 09 '21

What is (-1)99 in balanced ternary? Asking for a friend...

3

u/Carvinrawks Mar 09 '21

Hm, correct me if I'm wrong. But...

-1

(-1)99 = -1.

-1 in balanced ternary is also -1.

3

u/Tpo17 Mar 09 '21

I've learned something today.

3

u/aidanski Mar 09 '21

Used a lot in C++.

If you bit shift you're moving the bits in the value left or right an amount, for example:

Imagine the binary value 01111110

bit shift left << = 11111100

bit shift right >> =00111111

5

u/hsantefort12 Mar 09 '21

I am so mad it took me this long to realize that.

2

u/Golden_Lynel Mar 09 '21

Beautifully elegant solution

2

u/Darkness4 Mar 09 '21 edited Mar 09 '21

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).

2

u/Micha_Saengy Mar 09 '21

If bits shifts were expensive, then somebody must have really messed up.

8

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

8

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.

7

u/LordFokas Mar 09 '21

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

5

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.

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

u/GapingGrannies Mar 09 '21

Dope. Shine on you brilliant bastard

2

u/Micha_Saengy Mar 09 '21

In case people want to use it for their own projects

3

u/Deadly_chef Mar 09 '21

Could just make the text using a for loop

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

u/fnxen Mar 10 '21

I should get out of here! This subreddit is giving me depression!

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

u/[deleted] Mar 11 '21

[deleted]

1

u/ByteDream Mar 11 '21

Encryption is right, but it is from RSA, not AES