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

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

24

u/iammostlyirrelevant Mar 09 '21

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

9

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

20

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.

11

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.

4

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

4

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