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 8385677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:44:02+00:00 2026-06-09T17:44:02+00:00

Possible Duplicate: What does this & operator mean here? Why is the & in

  • 0

Possible Duplicate:
What does this & operator mean here?

Why is the & in this if statement right before the macro SDL_BUTTON? What Does it do? Help me understand what this code does.

if(SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1))
    //code to be executed
  • 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-09T17:44:04+00:00Added an answer on June 9, 2026 at 5:44 pm

    It is a bitwise “and” operation taking the result of SDL_GetMouseState(NULL, NULL) and “anding” it to the result of SDL_BUTTON(1).

    That is, the result is an integral value where all of the bits that are 1 in both answers will be 1 in the final result.

    Effectively, this is checking to see if the SDL_BUTTON(1) is currently pressed. They do this rather than an == comparison because this will evaluate to true when button 1 is pressed even in combination with other simultaneous mouse presses.

    Bitwise AND clarification

    Say I have the following: int foo = 25; I can view this in several different bases. The normal base is 10 (decimal); the base in which we look at numbers ordinarily in everyday life. In base 10, this number is 25. I can also look at the number in base 2 (binary). In binary, the number is expressed as 11001. That number can be interpreted as having several bits “true” (1) and several bits “false” (0). The “true” bits all get a fixed value associated with their place in the column. Each column has a value 2^i, where i is the index of the column. Columns are numbered from right to left with the right-most column being number 0.

    Truth value:     1    1    0    0    1
    Column value:   16    8    4    2    1
    

    We take the column value for all columns that are true and add them together to get the value. In this case, we will add 1, 8 and 16 together because they are in columns that are true. 1 + 8 + 16 = 25.

    We can perform other operations on binary numbers as well, such as “bitwise and”. In C, there are two types of and statements. Logical and and bitwise and. A logical and takes the form:

    if (a && b) { // two & together is logical-and
       // do something
    }
    

    The statement // do something is executed if a holds a value that can be interpreted as true and b holds a value that can be interpreted as true; for integers, all non-zero numbers are true. If either are false, the statement is not executed.

    Bitwise and is similar, but operates on a bit level. It takes two values and compares them one bit at a time to determine an output value. If the bit is true in both values, then the bit in the output is also true. If the bit is false in either value, then the bit in the output is false.

    int a = 25;    // 011001
    int b = 13;    // 001101
    int c = a & b; // 001001  -- or 1 + 8 == 9
    if (c) {
       // do something if c is non-zero
    }
    

    Your code above is taking the bitwise and of two values and then interpreting that as true or false. I don’t know the exact values used in SDL, but let’s say that SDL_BUTTON(1) as a value of 4 and SDL_BUTTON(2) has a value of 8. SDL_GetMouseState(NULL, NULL) will return an integer where each bit represents a flag. If SDL_GetMouseState(NULL, NULL) returns 4 (mouse button 1 is down), then 4 & 4 will be 4 (0100 & 0100 == 0100). If it returns 8 (mouse button 2 is down), then 8 & 4 will be 0 (01000 & 00100 == 00000; there are no columns with a 1 in common). If it returns 12 (mouse buttons 1 and 2 are down), then 12 & 4 will be 4 (01100 & 00100 == 00100).

    If mouse button 1 is pressed, then the result of the & will be non-zero, i.e. true. If mouse button 1 is pressed and mouse button 2 is pressed, the result is still true.

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

Sidebar

Related Questions

Possible Duplicate: What does T&& mean in C++11? For some reason, this is eluding
Possible Duplicate: How does this CSS triangle shape work? Please help me i need
Possible Duplicate: Reference - What does this symbol mean in PHP? I have a
Possible Duplicate: Reference - What does this symbol mean in PHP? What's the difference
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: What does new() mean? Like in title. I wonder what this syntax
Possible Duplicates: What does this expression mean “!!” What does the !! operator (double
Possible Duplicate: What does map(&:name) mean in Ruby? In Ruby, I know that if
Possible Duplicate: What does map(&:name) mean in Ruby? Ruby/Ruby on Rails ampersand colon shortcut
Possible Duplicate: What does this CSS shorthand font syntax mean? I was looking at

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.