08-15-2022, 12:00 AM
(08-14-2022, 07:38 PM)SierraKen Wrote: I have no idea what "bit shifting" means, except that the Wiki page says it divides it by 2 faster. Oh well, I try not to use things I don't understand unless I have to. But still interesting!
I don't think many people use bit shifting that often, it is a tad incomprehensible, and it's really only useful where there's an integer with an even power of two. It just happened to be ok for a diamond with a size of 20.
_SHR(variable%, x%) will divide variable% by 2 ^ x%, dropping fractions
_SHL (variable%, x%) will multiply variable% by 2 ^ x%,
I do it just to get used to it, really, for the eventual day where it actually might save some processor time in a fast application. It doesn't do well for precision division because, like an integer, it drops fractional results since it won't preserve overflow bits. I like it for dividing a screen into halves, quarters, eighths, etc., or for converting between LOCATE positions and _PRINTSTRING positions. In those cases one pixel one way or the other doesn't make a big difference on most screens and there isn't a half pixel anyway. You don't need it for most stuff, but it's cool to know it.
Example:
a% = 15 ' in binary that's a% = &B0000000000001111 (big endian form here for clarity)
b% = _SHL(a%, 1) ' shifting a% one bit to the left, b% = &B0000000000011110 or b% = 30
c% = _SHR(a%, 1) ' shifting a% one bit to the right, c% = &B0000000000000111 or c% = 7
If you shift around a lot without care, you lose precision quickly, for example:
Code: (Select All)
a% = 15
PRINT a%
b% = _SHR(a%, 1)
PRINT b%
c% = _SHL(b%, 1)
PRINT c%
If I want to put something a quarter of the way across a screen width I would do _SHR(_WIDTH(0), 2) for an X position, which is the same as _WIDTH(0) / 4
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
sha_na_na_na_na_na_na_na_na_na: