Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (dr_panther, 7th_zorro), 1,203 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rating: 5
Page 1 of 3 1 2 3
Tutorial: bitwise"&" operator use, **very useful** #18984
10/29/03 11:54
10/29/03 11:54
Joined: Mar 2001
Posts: 3,298
Beverly, Massachusetts
Rhuarc Offline OP
Expert
Rhuarc  Offline OP
Expert

Joined: Mar 2001
Posts: 3,298
Beverly, Massachusetts
This is a method that you find used commonly in C++ (like in the 3dgs sdk for example) but is rarely used in Cscript to my knowledge. Unfortunately, nobody realizes the power that this technique can have. It is used (in a rather advanced form) in weapons.wdl. The common use for this technique is to store several flags in a single number, by assigning each flag a square number. You are likely wondering how this works, and a little about the & operator, so I'll start right into some example code:
Code:
 var flaglist;

define _flag1,2;
define _flag2,4;
define _flagA,8;
define _flagZ,16;//all flags get assigned a unique square number


This is the basic setup we work from. Now, to set the flags:
Code:
 flaglist = _flag1 + _flagA; 


Now your flaglist is equal to 10. Later in the code, we want to find out which flags were put into the original value, _flag1 and _flagA in this case. This is where some blackbox code comes into play (you could work it down as to how it works, but I will not go into the binary equivilents to square decimal numbers). How in the world are we to acheive this?? The user could have put in 2, 1, and 7, how would we know after the calculation? This is why we work only with square numbers. Is there any other combination of square numbers that would equal 10 (without using one twice)? No, there isn't! Now, you can probably now see a complex method of reasoning this out with a lengthly series of IF statements like
Code:
 if(flaglist==10) { //then we know that flag1 and flagA were activated... insert appropriate code here

}


This can be tedious and entirely unneccesary. There is a lovely operator called the bitwise-AND which works at a binary level, which I won't go into the technicalities of. The thing is this: we can now test to see if any single flag was in the original addition of flags with one simple statement:
Code:
 if( (flaglist & _flag1) != 0) 


Note that this is different than using the && operator.
I'm trying to be fairly brief, so, a final analysis can be made from this code snippet that you can play with to your hearts delight.
Code:
 define _temp_flags,skill1;

define _transparent,2;
define _flare,4;
define _bright,8;
define _passable,16;
define _shadow,32;
define _user_rotate,64;
//add whatever else you would like for user-defined flags here

function setflagsInEnt()
{
if((my._temp_flags & _transparent) != 0) { my.transparent = on; }
if((my._temp_flags & _flare) != 0) { my.flare = on; }
if((my._temp_flags & _bright) != 0) { my.bright = on; }
if((my._temp_flags & _passable) != 0) { my.passable = on; }
if((my._temp_flags & _shadow) != 0) { my.shadow = on; }
if((my._temp_flags & _user_rotate) != 0)
{
while(1)
{
my.pan += time;
wait(1);
}
}
}

action test_object
{
my._temp_flags = _transparent + _bright + _shadow + _passable +_user_rotate;
setflagsInEnt();
}



I'll make this a little more understandable next time I'm online and can add to it.

EDIT: fixed a little copy/pase error.

Last edited by [Rhuarc]; 10/30/03 09:35.

I no longer post on these forums, keep in touch with me via:
Linkedin.com
My MSDN blog
Re: Tutorial: bitwise"&" operator use, **very useful** [Re: Rhuarc] #18985
10/31/03 11:15
10/31/03 11:15
Joined: May 2002
Posts: 757
NY, USA
GhostDude Offline
Developer
GhostDude  Offline
Developer

Joined: May 2002
Posts: 757
NY, USA
A very good explanation! I think this is very helpful, thank you Rhuarc!


Lead Programmer of Realspawn Productions AIM: gh0s7mp630 MSN: [Email]"mpratt630@hotmail.com"[/Email]
Re: Tutorial: bitwise"&" operator use, **very useful** [Re: Rhuarc] #18986
11/05/03 05:23
11/05/03 05:23
Joined: Nov 2002
Posts: 601
Indy
poke_smot Offline
User
poke_smot  Offline
User

Joined: Nov 2002
Posts: 601
Indy
This is a good read, please, continue...


Making guns illegal will save as many lives as making murder illegal.
Re: Tutorial: bitwise"&" operator use, **very useful** [Re: poke_smot] #18987
11/05/03 07:07
11/05/03 07:07
Joined: Mar 2001
Posts: 3,298
Beverly, Massachusetts
Rhuarc Offline OP
Expert
Rhuarc  Offline OP
Expert

Joined: Mar 2001
Posts: 3,298
Beverly, Massachusetts
I'm glad it helped you out. I'll try to get the next part together, which will go into double-flags.


I no longer post on these forums, keep in touch with me via:
Linkedin.com
My MSDN blog
Re: Tutorial: bitwise"&" operator use, **very useful** [Re: Rhuarc] #18988
11/20/03 03:16
11/20/03 03:16
Joined: Jul 2001
Posts: 129
New York, USA
O
Odin Offline
Member
Odin  Offline
Member
O

Joined: Jul 2001
Posts: 129
New York, USA
Very good explanation. This is underused by most and can be very powerful.

Re: Tutorial: bitwise"&" operator use, **very useful** [Re: Rhuarc] #18989
11/20/03 14:25
11/20/03 14:25
Joined: Sep 2003
Posts: 4,959
US
G
Grimber Offline
Expert
Grimber  Offline
Expert
G

Joined: Sep 2003
Posts: 4,959
US
cool, hope you continue this. & and % were always the 2 operands when i was learning C++ that i always struggled with ( and still do).

Hope you will cover them both


Re: Tutorial: bitwise"&" operator use, **very useful** [Re: Rhuarc] #18990
11/27/03 02:16
11/27/03 02:16

A
Anonymous
Unregistered
Anonymous
Unregistered
A



This is one of the most useful posts I have seen on this forum. It's basic, but could save a lot of people a lot of typing. One note, just 'cause I'm a picky math person: the "square" numbers mentioned are actually "powers of two" not squares. So for those of you wondering why 8 is on the list and 9 isn't, that's why.

Thanks for posting this, Rhuarc! You da man!

Re: Tutorial: bitwise"&" operator use, **very useful** #18991
11/27/03 07:29
11/27/03 07:29
Joined: Aug 2003
Posts: 1,125
Germany / Reckenfeld
Tobias_Runde Offline
Serious User
Tobias_Runde  Offline
Serious User

Joined: Aug 2003
Posts: 1,125
Germany / Reckenfeld
Very good, but please remember that && is faster than &.
But your explanation ist very good and useful!


-------------------------- www.3dgs.de
Re: Tutorial: bitwise"&" operator use, **very usef [Re: Tobias_Runde] #18992
11/27/03 09:47
11/27/03 09:47
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
I'm using & and | a lot to save variables and if-checks.

But it's also very useful for networking. If you ened to transfer controls or things like that you only need binary numbers - for checking whether a certain key is pressed or not e.g. Now you add it ll up in one variable and send it to the server. On the server you can retrieve the pressed keys by using &. This way you reduce bandwith usage.

Re: Tutorial: bitwise"&" operator use, **very usef [Re: FBL] #18993
11/28/03 04:01
11/28/03 04:01
Joined: Mar 2003
Posts: 5,377
USofA
fastlane69 Offline
Senior Expert
fastlane69  Offline
Senior Expert

Joined: Mar 2003
Posts: 5,377
USofA
I use it for equipment storage. In my project, the players can own only one of each item type; there is no stacking; Hence, I set each item type to a power of 2 like above. I can then add these items to a single var and get a unique var:
my_equipment= 2+4+8= 14; Thus 14 is a unique number indicating that I own items 2, 4, and 8.

I can then use the "&" command to check to see if I own that item. In an of itself not too amazing, but it sure is efficient and elegant.

What I really like is that using the | command I can add the item BUT ONLY ONCE. That is to say, when I add the item to my equipment I don't ADD like this in

my_equipment+= new_item; //no no no!

No. This suffers from having to institute checks and balances such that if you select the same equipment twice, you don't continuasly add an item. By using the | command

my_equipment= my_equipment|new_item;

no matter how many times you select the same item (for example with a mouse click), you can't set the flag more than once so it automatically doesnt' overset the var.

Hence, using one skill, I can store 19 unique items. Using two skills I can store 361 items, which I find is more than enough for me. While I have no use for it, you could conceivable set three skills to equipment storage (for a total of 6859 items!!!!) and take advantage of vector fuctions to manipulate your equipment.

Page 1 of 3 1 2 3

Moderated by  adoado, checkbutton, mk_1, Perro 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1