How do flags work in C?

Viewed 23485

Recently I have come across several examples of "flags" in C and C++, and I don't quite understand how they work. After looking at some source code I noticed that often flag values are defined in hexadecimal such as the following:

FLAG1 = 0x00000001,
FLAG2 = 0x00000010,

My intuitive suggests that these values are being combined. Do flags work by combining all the flags values into one int? If I had used both of these like FLAG1 | FLAG2 would the result be 0x00000011?

Do I need to create enums with bit offsets or can I use ascending integers Like:

FLAG1 = 1;
FLAG2 = 2;
4 Answers
Related