Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Parsing Flags Based on the Power of 2

Author
Message
anwserman
15
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 26th May 2011 12:36
Hello everyone!

I've never been able to figure out how to easily parse flags based on the power of two. Or, multiple select flags I should state.

Let's say I have 5 different flags, each with these values based off of the power of 2.

Flag 1 = 1
Flag 2 = 2
Flag 3 = 4
Flag 4 = 8
Flag 5 = 16

If I don't have any flags selected, I get a value of zero. If flags 1 and 3 are selected, I get a value of 5, so on and so forth. If flags 1 through 4 are selected, I get a value of 15.

How can I go about easily parsing this in DBPro, short of having to nested IF statements? I'm not sure what the 'official' name is for this type of data storage, but I'm plum clueless on how to do it :3

Thanks!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 26th May 2011 15:56 Edited at: 26th May 2011 15:56


With flags, you generally number them from 0, and there's a very good reason for this, as shown in the code.

Basically, you start with a value of 1, then specify how far to the left you want to shift it.

Setting flags can be just as easy:


In addition, if you're not too comfortable with the shifts, bitwise-and and bitwise-or operators, there are a set of functions that I provide in my plug-ins too (they handle 64 bit quantities, but can still be used for 32 bits too).

anwserman
15
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 26th May 2011 23:47
Ah ok, bitwise operations. Never did like these :3 Or, I should say never learned how to do them and got scared hahaha.

Anyway, I do have your plugin pack installed.


So, going by your plugins, with this code I would do something like...


Going by what I've read, 'varSetFlags' would be the combined total of all flags set, and 'varTestFlag' would be the specific value I'm looking for, such as 1 or 2 or 4 or 8, etc.?

Or is my logic WAY out of whack?
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 27th May 2011 00:44
No, you don't use powers of two. You specify the bit number instead.

So instead of using 'var && 4' to check if bit 2 is set (numbering from zero), you use 'bit get(var, 2)'.

Taking my code from earlier:


anwserman
15
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 27th May 2011 01:48
OH OK! I think I get it. Thank you Ian.
I'm looking at this from the numerical 'non-bit' perspective (it's what's most familiar to me :p)

Bit 0 would equal integer 1
Bit 1 would equal integer 2
Bit 3 would equal integer 4
Bit 4 would equal integer 8

So on and so forth?
Thank you very much for your help.
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 27th May 2011 03:01
yeah, answerman, the reason people use flags based on the power of two, is because you're dealing with binary numbers. If there weren't bitwise operations, flags like you're talking about would be way more annoying.

You basically have this (where %010101011 is a binary number and all other numbers are base 10)

%00000001=1
%00000010=2
%00000100=4
%00001000=8

picture integers as an array of boolean values (either true or false), so, "or"ing these flags together:

%0010 | %0100 | %1000 = %1110 = 14

so "14" means that the first flag is false, the next three are true, and the rest are false.

I don't see how it would make much sense without binary numbers :S


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
anwserman
15
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 27th May 2011 04:04
Thanks Fuzzy!
I'm still on probation so my posts aren't appearing ASAP - but thank for the awesome explanation! It makes much more sense know. I'm a smart guy but also pretty dense sometimes as well XD

Login to post a reply

Server time is: 2026-07-11 06:06:43
Your offset time is: 2026-07-11 06:06:43