Using Bitwise AND with Window Flags

What's the purpose of the bitwise AND operator (&) when checking window flags? Why can't we use == instead?

The bitwise AND operator (&) is used with window flags because SDL uses a technique called bit flags to store multiple boolean values in a single integer. Each bit in the flags value represents a different window state.

How Bit Flags Work

Here's an example where we set and check bit flags:

#include <SDL.h>
#include <iostream>

void ExplainFlags() {
  // Example flags value where multiple
  // states are true
  Uint32 Flags{
    SDL_WINDOW_FULLSCREEN_DESKTOP |
    SDL_WINDOW_SHOWN
  };

  // Check individual flags
  bool IsFullscreen{
    (Flags & SDL_WINDOW_FULLSCREEN) != 0
  };
  bool IsShown{
    (Flags & SDL_WINDOW_SHOWN) != 0
  };

  // Wrong way - this won't work!
  bool WrongCheck{Flags == SDL_WINDOW_FULLSCREEN};

  std::cout << std::boolalpha
    << "Is fullscreen: " << IsFullscreen
    << "\nIs shown: " << IsShown
    << "\nWrong check: " << WrongCheck;
}
Is fullscreen: true
Is shown: true
Wrong check: false

Using == wouldn't work because it checks if the entire flags value exactly matches a single flag. When multiple flags are set (which is common), == would return false even when the flag we're interested in is set.

Visualizing Bit Patterns

Here's a simplified example showing how bits work:

#include <SDL.h>
#include <bitset>
#include <iostream>

void ShowBitPatterns() {
  // For demonstration, using small
  // values instead of actual SDL flags
  constexpr Uint8 FLAG_A{0b00000001}; // Bit 0
  constexpr Uint8 FLAG_B{0b00000010}; // Bit 1
  constexpr Uint8 FLAG_C{0b00000100}; // Bit 2

  // Set multiple flags
  Uint8 Flags{FLAG_A | FLAG_B};

  std::cout << "Flag patterns:\n"
    << std::bitset<8>(Flags) << " (All flags)\n"
    << std::bitset<8>(FLAG_A) << " (Flag A)\n"
    << std::bitset<8>(FLAG_B) << " (Flag B)\n"
    << std::bitset<8>(FLAG_C) << " (Flag C)\n\n"
    << "Checking flags:\n"
    << "Has Flag A: " << bool(Flags & FLAG_A)
    << "\nHas Flag B: " << bool(Flags & FLAG_B)
    << "\nHas Flag C: " << bool(Flags & FLAG_C);
}

This pattern allows SDL to efficiently store and check multiple window states using a single integer value.

We cover bit flags and bitwise operators in a dedicated lesson:

Bitwise Operators and Bit Flags

Unravel the fundamentals of bitwise operators and bit flags in this practical lesson

Fullscreen Windows

Learn how to create and manage fullscreen windows in SDL, including desktop and exclusive fullscreen modes.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Desktop vs Exclusive Fullscreen
Why would we want to use desktop fullscreen mode if exclusive fullscreen has better performance?
Managing Different Aspect Ratios
What happens if a player has a monitor with a different aspect ratio than our game's resolution?
Smooth Fullscreen Transitions
How can I smoothly transition between windowed and fullscreen modes without the screen going black?
Restoring Display Settings After Crashes
When using exclusive fullscreen, how can I ensure the display settings are properly restored if my game crashes?
Saving Display Preferences
How can I save the player's preferred fullscreen mode and restore it the next time they launch the game?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant