We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › One int - many properties - flags
Page Index Toggle Pages: 1
One int - many properties - flags (Read 623 times)
One int - many properties - flags
Nov 28th, 2007, 5:17pm
 
I used Lindenscript a while ago and there was this odd parameter convention in it that got me thinking.

Often in Lindenscript you get a situation where you have to pass in several constants into one parameter:

Code:

myFunction(CONSTANT_A | CONSTANT_B | CONSTANT_C, someVariable);


Thus you set that an object can fly, have springyness, whatever with the constants you feed into one parameter. So I was thinking - neat trick - how does it work? I can't find any tutorials so I thought I would ask here.

My first guess is that each of these constants is a multiple of 2.

1
2
4
8
16
etc.

Thus for each bunch of parameters you can tell what has been set because the combinations of those paramters will always make a unique number.

But how to extrapolate those parameters? I could use a greater than and subtraction I guess for each parameter, but why the OR operator in Lindenscript - are they catching the use of OR, or does it serve a function in determining what parameters are submitted.

Perhaps I have the multiples of 2 wrong?

Your thoughts and suggestions please.
Re: One int - many properties - flags
Reply #1 - Nov 28th, 2007, 7:18pm
 
| is bitwise OR, not logical.. so each number is a single set bit.

So the way you test is to check the values of each bit in the passed int.

Here's an example:
Code:
int low=1;
int med=2;
int high=4;

myfunc(low | high); // sends 5 or 00000101 as an 8 bit value

void myfunc(int val)
{
if(val&high>0) // high is set
...
if(val&med>0) //med is set
...
if(val&low>0) //low is set
...
}
Re: One int - many properties - flags
Reply #2 - Nov 29th, 2007, 10:33am
 
This article is pretty comprehensive

http://www.gmonline.demon.co.uk/cscene/topics/misc/cs9-02.xml.html#N1036344255


The technique is not as alien as you might think.
I've been doing some Quake3 engine hacking lately and Carmack and the id boys do this all the time, and I guess this is standard practice in low-level game programming.
Re: One int - many properties - flags
Reply #3 - Nov 29th, 2007, 10:59am
 
Cheers, that hit the spot.

It's one of these tricks you don't find lying around in front of you and usually I'm wondering what the hell people are doing with bitwise stuff (even though my genetic algorithm library does it's magic bitwise).

Here's a working demo in P5 for anyone else who wants to learn:

Code:

int RED = 1;
int GREEN = 2;
int BLUE = 4;

void setup(){
getFlag(GREEN|BLUE);
}

void getFlag(int flags){
if((flags&RED) > 0){
fill(255,0,0);
rect(0,0,30,30);
}
if((flags&GREEN) > 0){
fill(0,255,0);
rect(30,0,30,30);
}
if((flags&BLUE) > 0){
fill(0,0,255);
rect(60,0,30,30);
}
}


Note the code throws an exception if you take the bitwise shizzle out of parentheses.

And in other languages you don't need to test against zero.

Actionscript:

Code:

var RED = 1;
var GREEN = 2;
var BLUE = 4;

getFlag(GREEN|BLUE);


function getFlag(flags){
if(flags&RED){
trace("RED");
}
if(flags&GREEN){
trace("GREEN");
}
if(flags&BLUE){
trace("BLUE");
}
}
Re: One int - many properties - flags
Reply #4 - Dec 1st, 2007, 2:48pm
 
Hey, nice you got it.

I know this goes against the hack / low-level nature of bitshifting but the first thing that came to my mind was to wrap all of that into a library for easy consumption.

Lazy, I know Smiley
Re: One int - many properties - flags
Reply #5 - Dec 8th, 2007, 3:20pm
 
One of the reasons people to prefer bit-packing to a list of booleans is that it if you have 32 bits of information you can actually use 32 bits in memory - on most architectures, for various reasons a boolean value actually has to take up a full byte in memory (I'm pretty sure this applies even if you're writing machine code, because of the way the hardware works), so as long as you plan on using four or more flags (but less than 32!) you can save memory by packing them into a single 32 bit int instead of storing the flags separately.  At the cost, of course, of less convenient access to each boolean value.  Tricks like this become substantially more cumbersome if you're on architectures with weird integer sizes, but luckily in Java this is not an issue.

That's the theory; in practice, you probably see a lot of this because it's a clever trick that's held over from the days when saving a few bytes was a huge deal (if you program to consoles or mobiles, it still is!).  Programmers love clever tricks, and this one actually leads to fairly nice APIs - it's a bit easier to set all your flags by connecting them with |s than it is to individually remember the name of each boolean that you want to set.  Probably one of the few examples where low level stuff can actually make high level stuff easier...
Page Index Toggle Pages: 1