Qbasicnews.com

Full Version: and instead of mod...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i've been looking through toshi's optimization article. I found the and instead of mod one, but can't get it to work with 20.

sx = x mod 20

i tried everything, but can't get it to work. is it possible to and 20?
Well first, in order for you to understand why it doesn't work with 20, let's try this example:

Suppose I had a function that takes 2 numbers. It looks at the digits of the second number, and if a digit is zero then it makes that same digit in the first number zero too, otherwise (not zero) it does nothing. So, for example:
Code:
PRINT MyFreakyFunction(12345, 10101)
would print 10305.

Now suppose I wanted to MOD 12345 by 10000. That's 2345, right? Now, here's how would you use my function to do the same thing:
Code:
PRINT MyFreakyFunction(12345, 10000 - 1)
Which is the same thing as this:
Code:
PRINT MyFreakyFunction(12345, 09999)
So the 1 gets dropped and you're left with 2345 - it works!

Now let's say we want to MOD it by 2. The answer should be 1 (because it's odd). Like this right? 2 minus 1 is 1, so:
Code:
PRINT MyFreakyFunction(12345, 1)
And it gives us 5 :-( Looks like it only works with powers of 10 (10, 100, 1000, etc...)

AND is a lot like my function, except it works in binary instead of decimal. In binary, you carry at 2 instead of 10. So the only digits are 1 and 0. Mine only works for powers of 10, and the AND method only works for powers of 2 (2, 4, 8, 16, etc...), for the same reason.
Quote:i've been looking through toshi's optimization article. I found the and instead of mod one, but can't get it to work with 20.

sx = x mod 20

i tried everything, but can't get it to work. is it possible to and 20?
I don't understand under what circumstances you are trying to use AND instead of MOD. I consider AND and MOD as two different functions.
1) MOD computes the remainder of the first argument divided by the second argument. For a statement like:
SX=X MOD 20
the result placed in SX is the remainder of X/20. If X were 25, the result would be 5.

2) An AND isa boolean operation which performs a logical AND on the bits of the two arguments. For a statement like:
SX=X AND 20
the result placed in SX is the logical AND of the bits of X with the bits of 20. If X were 25 then the AND looks like this:
[25] in binary is: 11001
[20] in bianry is: 10100
Result of AND is: 10000
As you know, an AND will only produce a 1 bit if both bits are 1 in the same bit position.

Under what circumstances did you intend to substitute AND for Mod? The only case I can think of is when you are dealing with powers of 2.
*****