The other day I was trying to code a small C++ programming using the SDL multimedia library, and I ran into this small snag which I eventually solved through trial and error. The issue is, I understand what I did to solve the problem, but I don’t really understand the nature of the problem!
The issue was with keyboard event handling in SDL. The code to handle a single key press to exit the program is straight forward and simple. [eventQueue is an SDL_Event structure]
//checks for keypress events..
if ( eventQueue.type == SDL_KEYDOWN )
{
//note: uses the boolean logical '==' equals operator..
if ( eventQueue.key.keysym.sym == SDLK_ESCAPE )
{
running = false;
}
}
In the above code, simply pressing the ESCAPE key on its own ends the main loop and causes the program to clean up and close…
However… The code needed to handle key presses that use modifier keys (shift/alt/ctrl) did not work correct with the ‘==’ operator. It took me ages to find out that I needed to use a bitwise AND operator instead of the equality (logical?) operator.
//checks for keypress events..
if ( eventQueue.type == SDL_KEYDOWN )
{
//note: requires the use of the bitwise AND operator..
if (( eventQueue.key.keysym.mod & KMOD_ALT ) && (eventQueue.key.keysym.sym == SDLK_F4 ))
{
running = false;
}
}
My confusion here comes from the fact that, when using the ‘keysym.sym’ member, the logical operator ‘==’ works fine, however, when using the ‘keysym.mod’ member, it was necessary to use the ‘&’ bitwise AND operator.
Now, if I had to guess, I would say that it has something to do with the fact that ‘keysym.sym’ only has to handle a single numerical value that represents a single key on the keyboard, while ‘keysym.mod’ has to deal with various combinations of shift, ctrl, and alt keys…?
To sum up my question: Why is this the case? Is there anyway to learn, other than trial and error, whether a certain piece of data will need to be compared with bitwise or logical/equality operators? Why is it that “keysym.sym == SDLK_F4” works fine, but “keysym.mod == KMOD_ALT” does not? Why would an operation involving decimal numbers have a different result than an operation that compares the bit values? Are there also situations where logical operations work and bitwise operations would not work?
The bitwise AND is somewhat special.
==checks for equality but the bitwise AND operator allows you to work with the individual bits of a number.Imagine your event was defined as a list of keys:
You can then check to see if a particular modifier is a part of the event pretty easily:
The bitwise AND is sort of like an
instatement. You can define your event as a binary number like this:Now, when you perform the bitwise AND, you can easily check to see if a certain modifier has been applied to the event, as modifiers are also represented as binary numbers:
You can construct an event like this using the bitwise OR:
Wikipedia sums up bitwise operations pretty well:
Basically, bitwise operators allow you to work with information stored in the bits of an integer efficiently.