just a simple question.
Some python functions I have seen are called like this, for example pygame:
pygame.display.set_mode((255, 255), FULLSCREEN)
This seems normal to me.
But when you want to use more than one argument, you must use |. For example:
pygame.display.set_mode((255, 255), FULLSCREEN | HWSURFACE | DOUBLEBUF)
When and why would you want this kind of calling? I have heard it is the bitwise OR operator, but it seems that is only for boolean values. How does this work?
They're flags for different options. Each flag is just a number, specifically a power of 2. You use the bitwise operator
|
to flip the bits for all the flags you want. An example might help:so if
re
wants to know whether theIGNORECASE
flag is set it can just check whether the second bit(for 2^1) is equal to 1. If so, we should ignore case. And if it wants to know whether to beVERBOSE
, it checks the 7th bit(for 2^6). Byor
ing 2 and 64 together, you have a number with the second and seventh bits flipped.We can see that 66 triggers flags for 2 and 64, but not 8.