Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8371523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:10:55+00:00 2026-06-09T14:10:55+00:00

The other day I was trying to code a small C++ programming using the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:10:57+00:00Added an answer on June 9, 2026 at 2:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The other day this code was working. I changed some things and re-ran it
The other day, I came across this construct: static_cast<size_type>(-1) in some example C++ code,
The other day I saw a colleague of mine using sort to sort a
In Visual Studio (C++) the other day, I was trying to build some example
This short piece of code displays the date of the first day of the
G'day, OK, I have now rewritten this question totally: I am trying to import
I'm trying to refresh my main view in this code. It shows the right
I'm trying to create a small proof-of-concept application for my boss, however the code
I can't seem to get this code working. Im trying to make it so
I came across a weird scenario in my code the other day: I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.