What do the OR and AND in these snippets mean?

Blitz3D Forums/Blitz3D Beginners Area/What do the OR and AND in these snippets mean?

iprice(Posted 2011) [#1]
I haven't coded in Blitz3D for quite a while now and looking back through some sample code I found this -

[CODE]array(x,y-1)=array(x,y-1) Or 32[/CODE]

Surely the array will return a TRUE value as array=array. So what does the OR at the end do/mean?


The sample code also does something similar with AND -

[CODE]target=array(x,y) AND 31[/CODE]


I've never used OR or AND like this (they are usually part of an IF/THEN statement), so can anyone explain exactly what each snippet means?

Cheers.

Last edited 2011


Yasha(Posted 2011) [#2]
Surely the array will return a TRUE value as array=array.


You're actually being confused by two separate things here.

The first is that (assuming it's the whole line) that's an assignment, not a comparison. Comparison = is an arithmetic operator with a higher precedence than Or (so it would compare then do the Or operation), but assignment is a statement, not an expression, and has lowest precedence of all infix operators, so the array is being assigned the value of the Or expression. This may not have been obvious if you didn't know the second thing, which is...

Or and And (and Xor and Not) aren't some kind of special control flow construct - they're actually just mathematical expressions same as + and -. They're bitwise operators, which means they perform their logical operation on the bits of the integer values passed to them. What they actually do is similar to the logic you're familiar with, but at a lower level:

- "And" compares each bit of the two numbers and in the result number, places a 1 at that bit if both values had a 1, and an 0 otherwise
- "Or" compares each bit of the two numbers and in the result number, places a 1 at that bit if either value had a 1, and an 0 if neither did
- "Xor" compares each bit of the two numbers and in the result number, places a 1 at that bit if one value had a 1, and an 0 if neither did or if both did

To add to the confusion, "Not" is actually boolean, not bitwise, and will return 1 for 0 and 0 for any other value. The true bitwise Not operator is supplied as the unary ~ (which has the same precedence as unary +/-) - this flips each bit of the number passed to it.

Now that you can see that these can be used in arbitrary expressions, what are they good for?

Well, there are two main uses: combining, and masking, of bitflag or compound values.

Or is commonly used to combine values. In the expression %01 Or %10, the result will be %11 (that's binary notation in Blitz3D, by the way). If you have a bit-flag array, like the parameter to CreateTexture, this lets you ensure that the second bit is set to 1 without checking if it's set first. If you'd tried to use +, and the 2 bit was set, you'd get some unpredictable number (4 or 5) with the wrong set bits; this just ensures it's turned on. This is also the common way to combine ARGB colour values into a single integer.

"And" works the other way around. In the expression "flags And %10", the result will be just the second bit of "flags" - the other bits were compared to zero and thus lost, while that bit was compared to 1 and therefore if it too was 1, it still is (i.e. its value was preserved). So "And" acts as a mask, letting you chop out parts of a value you don't want to look at without affecting the ones you do.

Incidentally, this is where Shl and Shr come in handy. Say you wanted to get at the green component of a colour, you'd use colour And $00FF00 to mask out the red and blue. But the green value is 255 times larger than 255, which is its range. Shl and Shr literally shove bits left and right so you can convert them to the value you want, then shove them back up to the left of the "bit array" for storage.

So what's happening in those expressions above? In the first one, the sixth bit is being set to "on" in that array element (access, Or with flag, reassign), regardless of whether it was on before. In the second, the sixth and higher bits are being turned off, and the first five are left as they were. 32 is a single-bit number (being 2 ^ 5), so it can be used like this (generally people use the binary or hex notations to make this more visible though, especially for complex bit layouts).

In short? And and Or are actually arbitrary arithmetic operations that you can use anywhere a numerical expression is allowed. There are infinite combinations of uses for them, and now that you know this, I'm sure you'll start to see them too.

(Oh, and in case you were wondering - they're built-in CPU instructions - they're actually faster than +, -, etc. on some machines).

Last edited 2011


Charrua(Posted 2011) [#3]
exelent

if it helps in any way, i use two handy functions to clarify when i need to set/clear a particular bit on a variable:

;return the state 0 or 1 of the FlagNumber bit in Flags
Function TestOnOff(Flags, FlagNumber)
	Return (Flags And (1 Shl FlagNumber)) <> 0
End Function

;set the State of FlagNumber in Flags
Function FlagOnOff(Flags, FlagNumber, State)
	Local Mask
	Mask = 1 Shl FlagNumber
	If State=1 Then
		Return Flags Or Mask
	Else
		Return Flags And ( $FFFFFFFF Xor Mask )
	End If
End Function


using "FlagOnOff function" the two senteces you are asking for should be:
array(x,y-1) = FlagOnOff( array(x,y-1), 5, 1)


Target = FlagOnOff( array(x,y), 5, 0)


aside the particullar question, i supose you are trying to know what this senteces do in the program context, i supose that the array holds variables with 6 relevant flags (numbered form 0 to 5)

In the first case the code is Setting the sixth flag: number 5 and in the second one the code is clearing the sisth (and any other with higger number: in a 32 bit variable the sentence clerar bits from 31 to 5, letting bits 4 to 0 unchanged)

Juan


Rob the Great(Posted 2011) [#4]
Wow, very useful information.

I've seen the And-Or commands used like this all the time, but I've never really understood the process until now.

Is there a piece of code floating around somewhere which could better describe this with examples? Better yet, is there some sort of exercise I can try to solidify this in my mind. This is definately an area I could improve on.


The true bitwise Not operator is supplied as the unary ~ (which has the same precedence as unary +/-) - this flips each bit of the number passed to it.


Huh. I'll probably never use a bitwise Not operator, but it's fun to know it's there. Has it always been there? If I understand this right, this would be useful if you wanted to make a color-brightness invert filter, right? Man, I sound like such a noob right now.

Last edited 2011


Charrua(Posted 2011) [#5]
in the function FlagOnOff i use: "$FFFFFFFF Xor Mask" that operation performs a bitwise Not operation (indirectly) it should be done with "~Mask"

there's an example for using the "~" operator

I don't knew this one when wrote the FlagOnOff function, so i made the Not operation in an indirect way ("Not" operator didn't work and didn't know why until today!)

Juan

Last edited 2011

Last edited 2011


iprice(Posted 2011) [#6]
In my haste to post my original question I never even considered that the code statement wasn't a comparison! Stoopid!

There's a lot of info here to digest and some of it I definitely don't understand at the moment, despite programming for 25+ years! I've probably been doing similar things to AND/OR, but in a much less direct manner.

Cheers chaps :)

Now to have a play and get a better understanding.