#passing flags to a function
20 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
in libraries like SDL those flags usually represent one bit each, so they can just be OR'd together
well if they are actually flags you bitwise add them togheter
function(flag1 | flag2 | flag3);
yes but how they know to use all of them
oh, that part is an if statement based on some condition you want to check
so just a lot a lot if statements ?
you bitwise or them together, so it's one if statement per flag.
for example SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS
its different from SDL_WINDOW_BORDERLESS | SDL_WINDOW_SHOWN
yes, so you make a variable to hold the flags
well in this case
SDL_WINDOW_RESIZABLE is probably more of a you decision as a developer
should the window be resizeable or not?
but for example SDL_WINDOW_BORDERLESS might depend on if the user wants borderless full screen or windowed mode
oh i think i got it for example the bitwise is [01101] and there is option with [01001] it will only activate the second and last flags ?
enum flags : uint32_t {
a = 1 << 0; // 0x01
b = 1 << 1; // 0x02
c = 1 << 2; // 0x04
d = 1 << 3; // 0x08
e = 1 << 4; // 0x10
f = 1 << 5; // 0x20
g = 1 << 6; // 0x40
h = 1 << 7; // 0x80
};
uint32_t obj_state{ 0 };
// set the 3rd bit
obj_state |= flags::c;
// unset the 5th bit
obj_state &= ~flags::e;
// check if 1st bit is high
bool state = 0 != (obj_state & flags::a);
// count high bits
int32_t set_bits = 0;
for (uint32_t i = 0; i < sizeof(obj_state) * 8; ++i)
if (0 != ((obj_state >> i) & 0b1))
++set_bits;
you can do a bunch of stuff using bit flags, i think that would be most common examples there