Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do certain situations require the use of 'bitwise' operators instead of 'logical' / 'equality' operators?

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?

like image 737
MrKatSwordfish Avatar asked Oct 20 '25 16:10

MrKatSwordfish


2 Answers

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:

event = ['a', 'shift', 'ctrl']

You can then check to see if a particular modifier is a part of the event pretty easily:

if 'shift' in event:
  # ...

The bitwise AND is sort of like an in statement. You can define your event as a binary number like this:

event = 00010010

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:

  00010001  # event (18)
& 00010000  # shift key (8)
----------
  00010000  # you get a non-zero answer, so the shift key is in the event
----------

  00010001  # event (18)
& 00001000  # "z" key (4)
----------
  00000000  # you get zero because the "z" key wasn't a part of the event
----------

You can construct an event like this using the bitwise OR:

  00000001  # shift key (1)
| 10100000  # "a" key (160)
----------
  10100001  # resulting event (161)
----------

Wikipedia sums up bitwise operations pretty well:

A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast, primitive action directly supported by the processor, and is used to manipulate values for comparisons and calculations. On simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition. While modern processors usually perform addition and multiplication just as fast as bitwise operations due to their longer instruction pipelines and other architectural design choices, bitwise operations do commonly use less power/performance because of the reduced use of resources.

Basically, bitwise operators allow you to work with information stored in the bits of an integer efficiently.

like image 126
Blender Avatar answered Oct 22 '25 06:10

Blender


What you did here

eventQueue.key.keysym.mod & KMOD_ALT

is not a compare operation, it is a bit masking operation. The compare operation is implicit in C and C++: an expression that evaluates to zero means "false", all non-zero values mean "true". When used in a logic expression like yours, this is a shorthand for

(eventQueue.key.keysym.mod & KMOD_ALT) != 0

Now to the bit operations: certain values represent bit combinations of two or more values. For example, keysym.sym represents a combination of a bit pattern for ALT (which is itself a combination of the left and the right ALT) and whatever other key that may be pressed at the same time. To separate one value from a combination, a bit masking technique is used: a value that has ones in the bits of interest and zeros in all other bits (that's KMOD_ALT) is AND-ed with the combination value (in your case, it's keysym.sym) producing the bits of keysym.sym in the bits indicated by 1s of KMOD_ALT.

The net result is that eventQueue.key.keysym.mod & KMOD_ALT will be non-zero only if the ALT has been pressed.

like image 28
Sergey Kalinichenko Avatar answered Oct 22 '25 06:10

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!