OR ???

Blitz3D Forums/Blitz3D Programming/OR ???

Akat(Posted 2003) [#1]
what is this 'OR' stand for in the middle of the flags...
like this?

loadtexture("bla.bmp",1 Or 2)

???


GfK(Posted 2003) [#2]
loadtexture("bla.bmp",1 Or 2)
is the same as
loadtexture("bla.bmp",3)

Another example:
loadtexture("bla.bmp",1 Or 2 Or 8)
is the same as
loadtexture("bla.bmp",11)
Its basically combining the three separate flags into one value. You can also do it like this:
loadtexture("bla.bmp",1+2+8) etc...



Akat(Posted 2003) [#3]
just that??? geez, why coder bother using OR actually? that is the really question...


QuietBloke(Posted 2003) [#4]
If Im using bit flags then I use Or. When I look at the code its more obvious that you are dealing with bit flags.
Its just the way Im used to.
Plus the advantage is in cases where you want to combine sets of bit flags.

SOMETHING = 1 Or 2 Or 8

SOMETHING_ELSE = 1 Or 4

I can still quite happily use Or if I wanted to combine the two sets of flags.

NEW_FLAG_SET = SOMETHING Or SOMETHING_ELSE

whereas if I added them the result would be wrong.


Ice9(Posted 2003) [#5]
Isn't Or faster than an addition. This wouldn't matter
unless you were doing thousands of them. It's just coder
preference you see Or used a lot in windows code for
flags using the pipe symbol |.


Hotcakes(Posted 2003) [#6]
Probably a very common place you see Or is in an If...EndIf statement. It's all just based on bit maths, but reads nicely.

If variable=1 or variable=2 Then

first Blitz calculates this -equation-

result=variable And (1 or 2)

translates to

result=variable And 3

And is another thing similar to Or ;] in that it's a bit based dowhacky command. If result returns a value, the 'Then' part is executed, if result=0 after that equation, the 'Else' part is executed, if there is one.


Anthony Flack(Posted 2003) [#7]
Here is an explanation about how boolean logic gates work:

http://www.howstuffworks.com/boolean1.htm


Akat(Posted 2003) [#8]
what about this?...

if variable$="bla" or variable$="bli" then

how blitz work??? is it change the string to a value first?


Hotcakes(Posted 2003) [#9]
Nah it performs a byte-per-byte check on the contents of each string, so basically it breaks it down into

If (((Chr$(Mid$(variable$,1,1))=Chr$("b")) And (Chr$(Mid$(variable$,2,1))=Chr$("l")) And (Chr$(Mid$(variable$,3,1))=Chr$("a"))) Or ((Chr$(Mid$(variable$,1,1))=Chr$("b")) And (Chr$(Mid$(variable$,2,1))=Chr$("l")) And (Chr$(Mid$(variable$,3,1))=Chr$("i")))) Then

=] Meaning strings take a long time to be processed in an If...EndIf statement...


DrakeX(Posted 2003) [#10]
i think he's actually wondering why that comparison actually works in the first place.

well this is how the program does it when running:

it starts out with this.

if variable$="bla" or variable$="bli"

it then sees if variable$ is in fact "bla." if it is, that whole comparison turns into ("evaluates as") 1.

if 1 or variable$="bli"

same thing again.

if 1 or 0

(variable$ can't be both bla and bli at the same time now can it?)

it finally performs the OR on these two numbers. 1 OR 0 = 1.

if 1

IF then checks to see if its test is 1, and it is, so the code is run.

this is boolean logic.

and come on akat, you've used DB and you never understood what OR was in IF statements? ;)


Michael Reitzenstein(Posted 2003) [#11]
Hotcakes - I would presume Len is checked as well/first?


Hotcakes(Posted 2003) [#12]
It could be. But that would slow down every instance where the two strings are actually the same length. Who knows =]

To add to DrakeX's explanation, the If statement actually only checks if the value it's left with after all those calculations is not 0.


Akat(Posted 2003) [#13]
i know how or work... but really confusing me is how it can be turn into + operator in flag bit...


Anthony Flack(Posted 2003) [#14]
It doesn't turn into a + operator. I don't think you've been paying attention to how OR works at all.


<Death>(Posted 2003) [#15]
Anthony is right. OR works by setting the bits corresponding to its arguments. It is NOT an addition type command. For ex., 3+1 = 4, is not the same as 3 OR 1 (which is 3) it sets the bit you tell it to set.

EX. 3 + 1 = 4

00000011 (3)
+ 00000001 (1)
==============
00000100 (4)

in "OR" it's: 3 OR 1 = 3

00000011 (3)
OR 00000001 (1)
===============
00000011 (3) since the 0 bit was already set, no different outcome...

here's the diff: 2 OR 1 = 3

00000010 (2)
OR 00000001 (1)
===============
00000011 (3)

The 0 bit get's set and the resulting number is 3.

That's why it is so convenient to use it to set bits (flags, in most cases ordered within a byte as powers of 2).

If you use XOR, it toggles the bits in question.
(Good for figuring out if an item is in use, or something's status has been changed in a game)

The above is the bitwise OR, the logical OR usually evaluates down to some values being tested and the condition being true if any of the values evaluate to true. (like in an IF a or b then...) It comes down to a boolean and if any of the booleans are true the condition is met and execution branches accordingly. (See DrakeX's example)


JaviCervera(Posted 2003) [#16]
There is no flag with number 3 in Blitz, just because all the flags must be multiple of 2 to work, so, when we are talking about flags, + and Or does the same work.

The advantage of using Or is that you cannot accidentally add the same flag two times. Imagine that, instead of passing the flags directly into the function, you store them into a variable. It is easy to accidentally add the same flag two times when you're using +, if the code which sets the flags is too complex. If you use Or, if the flag has been added once, it won't add the number again to the flags.


DrakeX(Posted 2003) [#17]
try ORing 3 and 7. you'll get a rather different result than adding them.


JaviCervera(Posted 2003) [#18]
3 And 7 are NOT flags, because they're not multiple of 2. I've stated that in my previous post. I said that Or and + are the same FOR FLAGS.


Koriolis(Posted 2003) [#19]
not multiple of 2
I guess you meant "not power of 2" :)
Apart of that you're right it's better to use Or with flags rather than a '+': it's safer, faster (you won't ever notice it though) and more explicit (it says "I'm working at the bit level" unlike an adition).


marksibly(Posted 2003) [#20]
Hi,

'Or' is a 'bitwise operator' - it does a bitwise 'Or' of 2 integers. See Antony's link for details on how it works.

Basically, it 'merges' the bits in 2 integers, resulting in '1' bits whereever there was a '1' bit in *either* operand.

'And' is similar, only the result has a '1' where there are '1's in *both* operands.

Stuff like '<', '>', '>=' etc are themselves just 'operators', like '+', '-', '/' etc.

What they do is return a '1' if the comparison is true, or a '0' if its false, eg:

Print 5<10 ;prints 1
Print 5<3 ;prints 0

You can use bitwise 'Or' and 'And' with these results to achieve logic like:

a=15
If a<10 Or a>20
;do something.
Endif

...is the same as...
If 0 Or 0
Endif

...and you can even go...
a=10<20 ;=1


Koriolis(Posted 2003) [#21]
Wow, thanks for the post Mark!

For the ones that come froms C/C++ by example, it may be worth mentioning that because And & Or are bitwise operators (there is no logical And & Or in Blitz), you can't safely do like in C:
if (i && j) {...// do something if i & j are not 0

because in Blitz doing
If i And j Then ...
will do something if the bitwise and of i & j is not null, which is not the same. it's like using the '&' operator in C rather than '&&'. So always be explicit and do
If i<>0 And j<>0 Then..


Also is worth mentioning that Not, unlike And & Or, is a logical operator and not a bitwise operator: (Not 1) is 0 rather than $fffffffe. To do a bitwise Not, use Xor:
(1 Xor $ffffffff) = 1 "bitwise noted" = $fffffffe.


JaviCervera(Posted 2003) [#22]
I guess you meant "not power of 2" :)
Sorry, I am spanish and simply took a look at the dictionary to know how the spanish word "múltiplo" is said in english :)


Koriolis(Posted 2003) [#23]
Yes, but if I understand well, 'multiplo de 2' means (just like 'multiple of 2') a number in the form 2*n, where what you probably meant is 2^n (6 is a multiple of 2 but does not make a flag, while 8 does).
Or correct my poor level of spanish ;)
simply took a look at the dictionary
you stole my secret method! ;) (I'm french)


Akat(Posted 2003) [#24]
and u said faster... that's why i will stick with OR style in bit flag... no matter how it works now...


JaviCervera(Posted 2003) [#25]
then you're right, it's "potencia de 2" (power of 2) what i should have said... i've never been very good with maths :P