Nooby Confused about flags

Started by
7 comments, last by ibebrett 15 years, 11 months ago
I'm reading a book about game programming and the author uses flags in a section of his code. I was confused about what he meant by flags and looked it up on wikipedia. The article was a dense read and I still dont have an understanding of why you would use flags. It would be great if someone could show me a simple use of flags.
J.W.
Advertisement
I assume you're referring to bit flags.

You use bit flags to store a large number of boolean values using as little memory.

Imagine you want to keep track of eight boolean values. You could use eight integers (4 bytes * 8 = 32 bytes) or even better five bools (1 byte * 8 = 8 bytes). But can we do better?

It turns out we can. We can use one byte to store the eight states by simply using each bit in the number. Using binary operators & (and), | (or), ~ (not), ^(xor) we can manipulate integral types one bit at a time. For example:


char FLAGS = 0; // FLAGS = 0b00000000
FLAGS |= 0x01; // FLAGS = 0b00000001, Turn on bit #0
FLAGS |= 0x08; // FLAGS = 0b00010001, Turn on bit #4
FLAGS &= ~(0x01); // FLAGS = 0b00010000, Turn off bit #0

If you're not familiar with binary operators, make sure to not confuse them with the logical operators such as && (logical and) and || (logical or)

[Edited by - fpsgamer on May 11, 2008 1:24:43 AM]
What programming language is the book about? Because if it's about a basic language, like Blitz3D, or Dark Basic Pro, then I could help you a little more. But, if it's about anthing other than those two, I don't know much about those Object Oriented Programming (OOP)languages.

I think (maybe i'm wrong though) flags are just numbers, or stings (text) used with variables, functions, and commands in a programing language. They are (I think) used to set the command to do certain things that you choose.

For example (i'm making up code, just to give you an idea of how flags are used, so do not compile this code in any editor/compiler, it probably won't work.):
------------------------------------------------
global heightflag = 600
global widthflag = 800

WindowCommand(heightflag,widthflag,"Hello")

*Comment: the command syntax example is - WindowCommand(height,width,title string.*
------------------------------------------------

Hope this helps you!
Good luck with your programming!
As per fpsgamer's reply, you can set a number of a states in one or more bytes.

Essentially this is a much smaller and efficient way to ask for parameters in functions, as one example.

For example:
#define RENDERER_MIPMAPS 0x1 // first bit 0001#define RENDERER_HARDWARE 0x2 // 2nd bit 0010#define RENDERER_ANTIALIASING 0x4 // 3rd bit 0100#define RENDERER_TEX_FILTERING // 4th bit 1000 // our function that uses the flagsRenderer* CreateRenderer (unsigned int flags){	// with the & operator we're 'filtering' out only the 0x1 bit	// so if the first bit is 0 then the whole operation results in	// in 0 so therefore the if{} block isn't evaluated	if (flags & RENDERER_MIPMAPS) {		// enable mipmapping	}	if (flags & RENDERER_ANTIALIASING) {	}}// a test functionint main (){	// here we're adding together the 2nd and first bits to make 0011	Renderer* rdr = CreateRenderer (RENDERER_MIPMAPS | RENDERER_HARDWARE);	rdr->Destroy ();	return 0;}



& AND operator works like this:
If bit A and B are 1 then the result is 1, otherwise 0
A = 0010B = 0110--------C = 0010  <- only the 2nd bits in both A and B are 1 so this is the only bit that survives the AND operation


| OR operator works like this:
If either or both bits are 1 then the result is 1, otherwise 0
A = 0010B = 1100--------C = 1110  <- OR means that if A or B has 1 then the result has 1 so all three bits survive


^ XOR operator works like this:
If bit A is 1 and B is 0 or bit B is 1 and A is 0 then the result is 1, otherwise 0
A = 0010B = 0110--------C = 0100  <- only the third bit survives because only B has 1 and not A too




Individual bit results
A & B = C (AND)---------0 & 0 = 01 & 0 = 00 & 1 = 01 & 1 = 1A | B = C (OR)---------0 | 0 = 01 | 0 = 10 | 1 = 11 | 1 = 1A ^ B = C (XOR)---------0 ^ 0 = 01 ^ 0 = 10 ^ 1 = 11 ^ 1 = 0
The simplest you can boil it down to is that when you want to remember that your program has entered a certain state, you "flag" that the state has been entered.

How the "flag" is implemented (a simple boolean variable, or a single bit in some other variable) is irrelevant conceptually.

Essentially all a flag is, is a way for you to tell whether your program is in a particular state or not.

For instance (let's use a single boolean for simplicity's sake):

bool didWeFindItem = false;for(int i = 0; i < numItems; ++i){    if( items == itemWeAreLookingFor )    {        didWeFindItem = true;        break;    }}if( didWeFindItem ){    //do Something...}




The variable didWeFindItem is a flag. When we have found the item we're looking for, we flag that the item was found (by setting the variable to true). Then when we want to do something to the object you check to see if the flag value is set and proceed.
Quote:Original post by dashurc
The simplest you can boil it down to is that when you want to remember that your program has entered a certain state, you "flag" that the state has been entered.

How the "flag" is implemented (a simple boolean variable, or a single bit in some other variable) is irrelevant conceptually.

Essentially all a flag is, is a way for you to tell whether your program is in a particular state or not.

For instance (let's use a single boolean for simplicity's sake):

*** Source Snippet Removed ***



The variable didWeFindItem is a flag. When we have found the item we're looking for, we flag that the item was found (by setting the variable to true). Then when we want to do something to the object you check to see if the flag value is set and proceed.


Thats correct however flags (in C++ at least) more often refer to bitflags because you'd probably call a boolean flag simply a boolean.

Does it make sense now jdub? Its pretty simple when its explained visually. Just three simple operations between sets of bits. 'Tis a very handy feature of C/C++.

instead of asking for 5 boolean parameters in a function, use one 8-bit char (or better a 32-bit integer as its faster (32-bit CPUs like 32-bit integers)).
i think typically bit flags in c++ are used because of the lack of named parameter support, i highly doubt you are saving any time by packing 32 flags in one int.
It may not be faster but it saves on space and also is very useful when asking for a number of variable states.

// I'd rather have this:void CreateRenderer (unsigned int flags);// Than thisvoid CreateRenderer (bool antialiasing, bool mipmaps, bool filtering, bool render_texture, bool vsync, bool anisotropic filtering ...)


However with renderer creation you're more likely to ask for a structure than flags but hey, you'd probably have a flags value in that structure anywayz.
wouldn't it be nice to have named default parameters though

such as

void print(string text, niceFormat=false, bold=false, italics=false)
{
....
}

but the actual printing would go like this
print("hello")
print("hello", bold = true)
print("hello", italics = true, niceFormat = true)

oh well, different languages different styles

This topic is closed to new replies.

Advertisement