Anuken / Mindustry-Suggestions

Repository for Mindustry suggestions and feedback
131 stars 58 forks source link

Adding a bitwise operation #4975

Closed ZeleniyKustik closed 7 months ago

ZeleniyKustik commented 7 months ago

Describe the content or mechanics you are proposing.

We specify the bit number we want to start reading from and the total number of bits we want to read. Work example: We have a 64-bit number .....1111111100. We specify the bit number [2]. We also specify the number of bits we want to read [8]. As a result, we cut off all unnecessary bits and get 255 as output.

Describe how you think this content will improve the game. If you're proposing new content, mention how it may add more gameplay options or how it will fill a new niche.

This command will simplify operations with bits, which will allow you to write code based on binary logic faster. For example, a cellular automaton or data compression into something like an archive with the possibility of unzipping. Or just Tetris

Before making this issue, check the boxes below to confirm that you have acknowledged them.

camelStyleUser commented 7 months ago

really ez(if you get a predefined mask its even faster)

op shr tmp data start
op shl mask 1 length
op sub mask mask 1
op and out tmp mask

and if you didnt notice mlog was supposed to be assembly-like and i dont think processors have extract x bits from y offset operation

ZeleniyKustik commented 7 months ago

Thanks for the optimization and the reply

S0meM commented 7 months ago

I don't understand that. ⬆️But, I do understand that if you use the AND operation on a number and 15, then it will get you the first four bits of the number because of this:

(15 in binary) 00001111 AND (28 in binary) 00011100 -> (result in binary)00001100

You can do the same on any (2^x)-1 because they will all be a number of 0's with only 1's afterward.

camelStyleUser commented 7 months ago

I don't understand that. ⬆️But, I do understand that if you use the AND operation on a number and 15, then it will get you the first four bits of the number because of this:

(15 in binary) 00001111 AND (28 in binary) 00011100 -> (result in binary)00001100

You can do the same on any (2^x)-1 because they will all be a number of 0's with only 1's afterward.

1<<n is 2^n so (1<<n)-1 is (2^n)-1 and the data is just shifted right to get the right bits