Another ARGB bit shifting Q

Blitz3D Forums/Blitz3D Beginners Area/Another ARGB bit shifting Q

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here is why that works:

Lets say the value of each byte is 255.

Let's also look only at the last two bytes, G and B.

In memory, the bits for G and B would look as follows:

11111111 11111111

Now, the trick you're using first masks each of the bytes with $FE.

$FE looks like this in binary: 11111110

If we then AND (mask) our two bytes with this, we will get 0's wherever our mask is 0.

So for our two 255 values, we get:
11111110 11111110

This is the trick...

Shifting right divides a number. Shifting right by 1 divides it by 2. Shifting right by 2 divides it by 4... 3 divides by 8... etc. Powers of 2.

But if we want to shift all the bytes at once, we don't want the bits from teh green byte overflowing into the blue byte.

For example, if our bytes looked like this:

00000001 00000001

And we shifted right by 1, we would get:

00000000 10000000

Which is wrong, because now green is 0, but blue has become 128!

If we first mask off the bits that will overflow with 0 though, we can divide without worrying about the overflow, because the overflows will be 0 and will not affect the value of the bytes below!


What this means for you is that you can only do this particular trick if you want to divide by 2, 4, 8, etc.

Masking the TWO lowest bits of each byte and dividing by 4 would be equivalent to multiplying by .25.

But there is no equivalent to .75.

I don't think there is any way you could shift the bits that would be faster than masking off the bits and multiplying them individually for such values. Modern processors pretty much multiply as fast as they shift.