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
It is a bitwise “and” operation taking the result of
SDL_GetMouseState(NULL, NULL)and “anding” it to the result ofSDL_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 is25. I can also look at the number in base 2 (binary). In binary, the number is expressed as11001. 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.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:
The statement
// do somethingis executed ifaholds a value that can be interpreted astrueandbholds a value that can be interpreted astrue; for integers, all non-zero numbers aretrue. 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.
Your code above is taking the bitwise and of two values and then interpreting that as
trueorfalse. I don’t know the exact values used in SDL, but let’s say thatSDL_BUTTON(1)as a value of4andSDL_BUTTON(2)has a value of8.SDL_GetMouseState(NULL, NULL)will return an integer where each bit represents a flag. IfSDL_GetMouseState(NULL, NULL)returns4(mouse button 1 is down), then4 & 4will be 4 (0100 & 0100 == 0100). If it returns8(mouse button 2 is down), then8 & 4will be 0 (01000 & 00100 == 00000; there are no columns with a1in common). If it returns 12 (mouse buttons 1 and 2 are down), then12 & 4will 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 stilltrue.