Q:

Bitwise and Bit Shift operations

I did some research on Bitwise and Bit Shift operations to explain a code sample I found in the Minecraft Code:
public static int extractDragMode(int eventButton)
{
    return eventButton >> 2 & 3;
}

eventButton is always 0,1 or 2. Won't the Bitshift always make the left part 0 and make it return 0?

A:

Almost certainly you are mistaken and it's possible for eventbutton to be lots of different values.  The thing is, eventButton isn't interesting on its own.  I suspect that eventButton is actually several different values packed together.
This is a technique called bitmasking.  The eventbutton contains multiple pieces of information in a single int.  In this case, we are interested in the drag mode (judging by the method name extractDragMode).  It is the drag mode that will be 0, 1 or 2.  This only requires two bits and these two bits are the 3rd and 4th bit of the int.  By shifting them two bits right we get them into the first two positions.  Then we and with 3 to zero all the other bits in the int.  All we are left with now is if the value of the drag mode bits.

