How To

Can I do a fast bitshift in code (not RAM)

1

SP4CEBAR 2022-07-19 11:55 (Edited)

2^N is equivalent to a bitshift, using a bitshift is way faster, but the only bitshifts NX has will only affect RAM

Using the cycle counter

V=2^N

is faster than

ROR A,N
V=PEEK(N)

So the RAM bitshift won't be faster in my case


nathanielbabiak 2022-07-20 03:43 (Edited)

The only thing faster would be an array lookup, but it requires initialization:

DIM GLOBAL BITSHIFT_N2V(23)
FOR N=0 TO 23
  BITSHIFT_N2V(N)=2^N
NEXT N

After initialization, you'd use:

V=BITSHIFT_N2V(N)

All that said, your code might not do what you expect, since those two code snippets you posted don't do the same thing (there's a difference between a rotation and a shift). If you intend rotation, the console's native instructions will be fastest.


SP4CEBAR 2022-07-20 07:43 (Edited)

Performance isn't that important in my case
So rotation will put the bits on the other side (LSB will become MSB or vice versa) and shifting will cut off some bits, is that what the difference is?


Timo 2022-07-20 18:55

Rotation uses only one byte: it shifts all 8 bits and the ones which moved out from one side come in again from the other side.
The ROR and ROL command were mostly thought for video RAM (characters).


SP4CEBAR 2022-07-21 08:41

Thank you


Log in to reply.