Bitwise versus conditional And/Or
BlitzMax Forums/BlitzMax Programming/Bitwise versus conditional And/Or
| ||
The 'And' and 'Or' results seem backwards to me. i.e. the conditional And gives the same result as the bitwise |. Is this a bug or the expected behyavior?Print "16 And 255 "+ String(16 And 255) Print "16 & 255 "+ String(16 & 255) Print "16 or 255 "+ String(16 Or 255) Print "16|255 "+ String(16|255) Last edited 2012 |
| ||
If I recall correctly (can't check right now) the And and Or operators in BlitzMax are short-circuiting (i.e. they don't bother to evaluate the second operand if they can prove after the first whether the result will be false or nonfalse), like their counterparts && and || from C, so you can't rely on their results to be valid in any capacity other than the boolean: zero, or non-zero; as they won't actually perform all of the computation- and even if they do you wouldn't want Boolean And to return the same thing as Bitwise And lest it mask two nonzero values against each other and create a zero. (What results are you actually getting? What were you expecting? And is the order of evaluation even defined for BlitzMax, or is this an unpredictable optimisation?) Last edited 2012 |
| ||
And and Or are logical operator that means that for "and", if both numbers are none zero, The comparison is none zero(or true). The returned value such as in your case 255 or 16 is unimportant. the & and | are binary operators which mean that they work at the binary level. for example 7 which is equal to %111 and 2 which is equal to %010 in binary value to compare then: ...........%111 ...........%010 & =========== ...........%010........... = 2 which is the correct result .......... %111 .......... %010 | =========== ...........%111.......... = 7 which is the correct result Last edited 2012 Last edited 2012 |
| ||
...........%111.......... = 3 which is the correct result Not sure what you're on about there. %111 is 7, not 3. |
| ||
The bitwise results are what you would expect. The conditional results are more that a little confusing, but correct in their own way. The conditional operators are meant to be used with True/False operands. A different interpretation is applied to other nonzero values, they are all regarded as true. The short circuiting is done in way that looks a little strange, but perhaps is faster. A And B, the A is evaluated first. If A is nonzero then the result is B. Similarly, A Or B yields A for any non-zero ( i.e. true ) A. This explains the results in the original example. There are no surprises if A,B are restricted to boolean values. In other cases you must interpret the results according to the rule that any non-zero value is true. But you were already doing that when you used non-boolean A,B. |
| ||
ups! thats what I get for doing it in my head. corrected. |
| ||
What I was getting was; "16 And 255 " and "16|255 " both return 255 while "16 & 255 " and 16 Or 255 both return 16 I am use to Blitzplus where And and Or are bitwise operators. I guess the take away lession is that they're not interchangable in BlitzMax which is probably better as the bitwise behavior cause problems for logical comparisons. |