How To

How do bitwise operators react to math operators (in algebra)


SP4CEBAR 2022-04-23 16:19

To optimize my matrix graphics I want to try to write multiple bits of a character byte at once, I figured that drawing little horizontal one byte lines would be a good way to do this.
To make that happen I've written this calculation:
V=-1+2^(8-(X MOD 8)) AND 256-2^(7-(X1 MOD 8))
The left part of the calculation generates the left part of a line made out of bits, and right part of the calculation generates the right part of the line
The two parts are connected with a bitwise AND which will turn it into a line where I can control where it starts and ends

It really looks like this could be simplified, but I don't know the algebra rules around bitwise AND: like can I take out the "-1" and the "256" and calculate them and can I merge the two 2^... parts

It may not be possible to compact is, but if it can be done, that'd be really nice

Timo 2022-04-24 07:50

It’s probably OR instead of AND, but I cannot think more now, because I’m on holidays ;)

was8bit 2022-04-24 14:43

Long portions of math make my brain flatline.... i usually have to split mine all apart so i can more easily see each portion better... sorry i cant help here :(

SP4CEBAR 2022-04-25 06:34

It's AND because it's the part which both inputs have in common: one part sets the left limit and goes all the way to the right, the other sets the right limit and goes all the way to the left
As far as I know it's impossible to simply it, but maybe there's a trick

rilden 2022-04-26 19:32 (Edited)

You can calculate all possible results at the start of your program and put them in an array like this:

FOR I=0 TO 7
  FOR J=0 TO 7
    BITMASK(I,J)=-1+2^(8-I) AND 256-2^(7-J)

Then you can do: V=BITMASK(X MOD 8, X1 MOD 8)

SP4CEBAR 2022-04-28 18:00

@rilden, thank you!

Log in to reply.