Home > Software design >  C: do bit flags have a priority order?
C: do bit flags have a priority order?

Time:08-20

I was reading SDL2 documentation, in particular SDL_CreateWindow function and the possible flags for its flags argument: SDL_WindowFlags.

Since there are flags that are "mutually exclusive", for example SDL_WINDOW_MINIMIZED(64) and SDL_WINDOW_MAXIMIZED(128) I assume that one must be applied before the other or one gets ignored.
Therefore, if we set both of them as follows:

SDL_CreateWindow(WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);

the result is that the window gets minimized when the application starts.

So my question is: is there some kind of order priority rule?

I apologize if that's a duplicate, but couldn't formulate the question well enough to find the answer anywhere else.

CodePudding user response:

Bit flags in C don't have a priority order. A bit flag is perfectly happy to have the bits 128 and 64 at the same time. The compiler doesn't even know that 128 means maximized and 64 means minimized!

The SDL_CreateWindow function, however, will have its own priority order according to the creators of SDL. The relevant source code can be found here:

    if (flags & SDL_WINDOW_MAXIMIZED) {
        SDL_MaximizeWindow(window);
    }
    if (flags & SDL_WINDOW_MINIMIZED) {
        SDL_MinimizeWindow(window);
    }

so if you use both the maximized flag and also the minimized flag, SDL (not C) will maximize and then minimize the window, so it ends up minimized.

  • Related