ID:2191698
 
I don't know what this does, at all. I'm looking at code that uses it, and reading its helpfile doesn't really tell me much more. Could someone explain the particulars of its common use and less common uses to me? Thanks in advance.
Bitflags:
#define STATE_RUNNING 1
#define STATE_JUMPING 2
#define STATE_SWIMMING 4

var states = 0
// turning on the STATE_RUNNING flag
states |= STATE_RUNNING

// tuning off the STATE_RUNNING flag
states &= ~STATE_RUNNING


You can also use it with list adding to ensure only a single entry exists for the item:
// add the string "thing" to the list if it isn't already in there
some_list |= "thing"
So what does states equal if you |= STATE_RUNNING?

states = 0 | 1 ?
0 | 1 = 1

So states = 1 if you |= 1 it?

Does 0 | 1 | 4 = 5?
In response to Gooobai
Gooobai wrote:
So states = 1 if you |= 1 it?

Does 0 | 1 | 4 = 5?

0 = 0b0000
1 = 0b0001
4 = 0b0100

So...
0000 | 0001 | 0100 = 0101
so yes, 5

a =| b is shorthand for a = a | b

In terms of bits and binary operations,

"|" is the bitwise OR operator, meaning when you OR two values, the returned value is a set of values from either.

This means that if a = 0b0101 (5) and b = 0b1001 (9)
then a | b = 0b1101 (13).

0101 | 1001 = 1101
Right, if you'll notice in his example above that he's doubling the value of the flag each time, the numbers between those values are used to determine which flags are enabled or disabled.

if(states&STATE_RUNNING) // If they're running
if(states&STATE_SWIMMING) // If they're swimming.

if(states & (STATE_RUNNING|STATE_SWIMMING)) // If they're doing both.