GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


flags explanation by baldurk
// I'm going to write an article about this when I can get my ass in gear.
// for now though, here's a quick and dirty guide to flags

#define FLAG_NUMBER1 1<<0
#define FLAG_NUMBER2 1<<1
#define FLAG_NUMBER3 1<<2
#define FLAG_NUMBER4 1<<3
#define FLAG_NUMBER5 1<<4
#define FLAG_NUMBER6 1<<5
#define FLAG_NUMBER7 1<<6 // I didn't say it would have imaginative names

// they look something like this:

0000001 // FLAG_NUMBER1
0000010 // FLAG_NUMBER2
0000100 // FLAG_NUMBER3
0001000 // FLAG_NUMBER4
0010000 // FLAG_NUMBER5
0100000 // FLAG_NUMBER6
1000000 // FLAG_NUMBER7

// let's OR some together with |

int foo = FLAG_NUMBER4 | FLAG_NUMBER6 | FLAG_NUMBER7;

// what is foo now?

0001000 // FLAG_NUMBER4
+
0100000 // FLAG_NUMBER6
+
1000000 // FLAG_NUMBER7
=
1101000

// see? simple. | simply adds the bits together.
// It's more complex if you don't use numbers that only have one bit in them,
// but it's less useful that way so we can ignore it for the moment

// now to extract the information

if(foo & FLAG_NUMBER4)
{
    // 1101000 is foo
         &
    // 0001000 is FLAG_NUMBER4
         =
        0001000

    // see? simple. & simply lets through bits that are the same.
}

// to show what would happen if we tested against another one

if(foo & FLAG_NUMBER2) // would fail
{
    // 1101000 is foo
         &
    // 0000010 is FLAG_NUMBER2
         =
        0000000

    // because none of the bits are the same, we get 0, so the if fails.
}