What's the meaning of this |=?

Posted By: mygame4

What's the meaning of this |=? - 04/11/10 19:19

I have a silly question smile

I found some scripts have |=
For example, p.flags |= (BRIGHT | MOVE);
or p.flags |= MOVE;
I don't know what's the meaning of |=
Posted By: Widi

Re: What's the meaning of this |=? - 04/11/10 19:24

here you go
Posted By: mygame4

Re: What's the meaning of this |=? - 04/12/10 00:45

Thanks, but I can hardly understand what it is for.
Posted By: Xarthor

Re: What's the meaning of this |=? - 04/12/10 05:21

Well this | is a so called "bitwise" or.
If you have a binary number like 0110 and another binary number like 0100 you can use the OR to connect them and "calculate" a resulting number.
In this case:
0110 | 0100 = 0110
What you do:
You look at each "position" in both number strings and if one of them 1 you write a 1 to the result:
0 | 0 = 0
0 | 1 = 1
1 | 1 = 1
0 | 0 = 0
result = 0110

The |= operatator does the same but also includes an assignment to another variable.
a |= b
is the same as:
a = a | b

Hope this makes some sense to you.
Posted By: Widi

Re: What's the meaning of this |=? - 04/12/10 09:20

The flags in 3dgs (SHOW, POLLYGON and so on) are all saved in one Bit. With this Operator (|=) you can set only the bit you need. (SHOW is set now).
Posted By: Aku_Aku

Re: What's the meaning of this |=? - 04/12/10 20:23

In Lite-C there is an operator to make decision about equality.
In this language, as like as all "C" like languages, this operator is the "==".
Sometime in programming we need to examine the reversed equality for this purpose is being the "|=".
So just imagine
  • 1. == equal
  • 2. |= not equal
  • 3. >= greater than
  • 4. <= less than

and so on...
Posted By: DJBMASTER

Re: What's the meaning of this |=? - 04/12/10 20:30

^ That's wrong, != is the not equal comparison, not |=.
Posted By: Aku_Aku

Re: What's the meaning of this |=? - 04/13/10 16:55

True, that is my fault. I am sorry.
Posted By: Widi

Re: What's the meaning of this |=? - 04/13/10 18:05

Now is:
Aku_Aku == true; wink wink
Posted By: zeusk

Re: What's the meaning of this |=? - 04/14/10 18:24

Originally Posted By: Widi
Now is:
Aku_Aku == true; wink wink

Posted By: Alan

Re: What's the meaning of this |=? - 04/14/10 19:17

@mygame4: To make things easier for you to understand I'll try to explain it in a different way.

In 3D GameStudio, every entity you create - it could be a model of your player character for example, but also a sprite, a tree... - has a fixed set of so-called "FLAGS". A flag is very much like a "sign" the entity gives to the A7 engine. For example the "visible" flag tells the engine whether the entity should be visible or not, the "passable" flag tells the engine whether this entity should be used for collision detection or not etc.

When it comes to flags, it is always a question of "is it set?" - yes or no. Now, you don't really need a whole integer number to store a value of zero (for "no") or 1 (for "yes"), it would be a waste of memory, so several flags are now stored in the same integer number.

You have to know three things about a flag:
- the name
- what it does
- how to set/reset it

You can look up the name of a flag in the manual. There it also says what the flag is for. Just search for "flags". And for setting a flag - in liteC you can write this:

Code:
set(my, VISIBLE);



This would set the visible flag of the entity that is currently referred to in the "my"-pointer. You can exchange "visible" with any other flag you can find in the manual - just be aware of the consequences. If you want to set a flag to "off", you can use this:

Code:
reset(my, VISIBLE);



This sets the flag back to "off" state.


Now, back to your question. What does |= ?
Well, writing this:

Code:
my.flags |= visible;



...is exactly the same as...

Code:
set(my, VISIBLE);



So instead of the "set()" function you may also use |=. But if you ask me, you shouldn't do that because it makes your code harder to read. But just in case you encounter |= in other people's code, now you know, basically it is for setting a flag (actually there ARE other ways to use it, but I've never seen it anywhere). By the way, for re-setting a flag, you can use &=. However, I'd recommend the "reset()" function instead.


I hope I could help you.


Greets,



Alan
© 2024 lite-C Forums