Bit flags in Zdoom

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
Locked
User avatar
Apothem
Posts: 2070
Joined: Sat Nov 29, 2003 7:13 pm
Location: Performing open heart surgery on an ACS compiler.

Bit flags in Zdoom

Post by Apothem »

I've been re-reading how bitflags are done in zdoom, and I'm finding that my work isn't quite translating correctly.

Below is what I've been doing to manipulate a bit flag in zdoom:

Assume flags are defined as follows:

Code: Select all

#define flag1 1
#define flag2 2
#define flag3 4
#define flag4 8
#define flag5 16
#define flag6 32
#define flag7 64
#define flag8 128
To add flags to a variable:

Code: Select all

variablename |= flags;
To remove flags from a variable:

Code: Select all

variablename &= ~flags;
but for some reason, this is not working. I was googling it a bit, and realized that the bitflags are defined differently in C/C++ and wanted to get an opinion from people who've worked with the source to see if the way I'm doing it wrong.

Whenever I try to turn off flags or toggle them, this doesnt seem to work. However, checking flags that are active does seem to apply correctly. That is, unless the checks just aren't working correctly.

If more code if needed, I can post it. I'm just working on an old module and don't have enough of it separated to provide a good working example at the moment.

Article I'm referencing for this:
http://forum.codecall.net/topic/56591-b ... h-example/
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49223
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Bit flags in Zdoom

Post by Graf Zahl »

Of course more code is needed. Who knows what you did wrong without posting anything checkable at all...?
User avatar
Apothem
Posts: 2070
Joined: Sat Nov 29, 2003 7:13 pm
Location: Performing open heart surgery on an ACS compiler.

Re: Bit flags in Zdoom

Post by Apothem »

Graf Zahl wrote:Of course more code is needed. Who knows what you did wrong without posting anything checkable at all...?
I get that, I'm more asking about it in general. As in, how are you SUPPOSED to be doing it in doom? I've read something close to 3 different ways of doing it. However, making a mockup example of what I was doing seems to be working on the single flag level, but combining flags doesnt seem to work.

Also thank you for the quick response Graf :)

In the process of making my example, I think I see what I've been doing wrong. So I need to rephrase my question :P

How do I check multiple bitflags within a single statement?

right now I've been doing:

Code: Select all

if (bitflags & flag1|flag2) //if flags 1 and 2 are active
{
//do stuff
}
But this clearly doesnt work, as it is expecting the value to just be non-zero. Judging from my working example, how are multiple flags supposed be checked? One at a time? Or can I combine them to check multiple values at once?

Working example code below, puke respective scripts for basic results. Wad provided in drop box link.
Spoiler:
https://www.dropbox.com/s/o3ekoqrbi9upe ... t.wad?dl=0
User avatar
oODemonologistOo
Posts: 318
Joined: Tue Jul 17, 2007 10:30 am
Location: one of the labs of hell

Re: Bit flags in Zdoom

Post by oODemonologistOo »

Use (bit & (flag1 | flag2)) instead. Operator precedence applies.
User avatar
Apothem
Posts: 2070
Joined: Sat Nov 29, 2003 7:13 pm
Location: Performing open heart surgery on an ACS compiler.

Re: Bit flags in Zdoom

Post by Apothem »

oODemonologistOo wrote:Use (bit & (flag1 | flag2)) instead. Operator precedence applies.
Yup, that got it. Thanks a bunch.

:lol: wow didn't know what i was missing was so obvious.

So in the case of checking the condition of flags, should i be enclosing the entire bitflag statement within () as well?

Code: Select all

if (!(mybitflagvariable & flag))
{
//do stuff
}
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49223
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Bit flags in Zdoom

Post by Graf Zahl »

Yes. The & and | operators have relatively low precedence so if they get combined with others, parentheses are always needed.
Locked

Return to “Editing (Archive)”