Binary Shifting

BlitzPlus Forums/BlitzPlus Programming/Binary Shifting

You can multiply X by 800 with shifting like so:

X SHL 9 + X SHL 8 + X SHL 5

Ie:
X*512 + X*256 + X*32 = X*800


A quick test however for this specific case shows that floyd is correct. Running a loop approximately 65 million times, the shifting method for multiplying by 800 takes 280 milliseconds on my AMD Athalon XP 2400+, whereas multiplying by 800 the normal way takes only 200 milliseconds.

Furthermore, both multiplying X by 256 and shifting X by 256 65 million times takes on average around 180 milliseconds.

In other words, there is no speed benefit to shifting bits on AMD processors in Blitz, and in fact there can be a penalty to doing it if you try to be clever.

I used to code on 386's, and in those days shfiting bits was really important, but I guess these days... You're not going to see any real speed boost by avoiding multiplications.

But it's still not a bad idea to do bit shifting for fitting bytes together and stuff.