Question

I'm new to bitwise operator, I don't know what are they use for, but I know the following:

5 & 3 = 1

because

0101  & 0011 = 0001

So yeah, I understand that we multiply each bit pair like 1 x 0 = 0, 1 x 1 = 1 and 0 x 0 = 0

Now when it comes to coding, I found the following code for onTouchEvent:

@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction() & MotionEvent.ACTION_MASK;
switch(action) {
    case MotionEvent.ACTION_DOWN : {
        break;
    }

    case MotionEvent.ACTION_MOVE : {
        break;
    }

    case MotionEvent.ACTION_POINTER_DOWN : {
        break;
    }

    case MotionEvent.ACTION_POINTER_UP : {
        break;
    }

    case MotionEvent.ACTION_UP : {
        break;
    }  
}
return true;
}

Ok firstable, the part int action = event.getAction() & MotionEvent.ACTION_MASK; what is the value of action after the operation, and what does it means? why not just simply use action = event.getAction() what's the meaning.

second, I've never seen the use of {} to define a code block for a case: is this specific because we're using the bitwise and operator or this is something totally normal and I happen to just notice that you can use them?

Was it helpful?

Solution

The reason the event is masked is to remove unrelated bits from the event. Performing bitwise-and with ACTION_MASK isolates only those particular bits. This is how you combine and test status flags that are packed into an integer. If you did not mask it, then if any other bit is set on the event, you would not be able to easily test your actions for equality.

What this is actually doing is reserving a small number of bits for an action, like having a 3-bit integer (or whatever it actually is). The mask just sets the relevant bits, so that all other bits are thrown away (ie are unimportant).

As for the braces in case statements, that's fine. You can put braces just about anywhere - it creates a a block-scope level, where any new variables you create inside that block will only exist in the block. In case statements this is quite normal, and prevents a variable in one case from "falling through" to subsequent cases. For example:

switch(foo)
{
    case 0:
       int i = 42;
       // do something with i
       break;

    case 1:
       // The variable i is still in scope here, which is often not
       // intentional (but can sometimes be useful)
}

To stop i from falling through:

switch(foo)
{
    case 0:
       {
           int i = 42;
           // do something with i
       }
       break;

    case 1:
       // The variable i defined above is not available here.
}

In your case, the braces are superfluous, but are probably there because the person who wrote the code has adopted this practice into their coding style. Most likely they've been burned by an unwanted side-effect before, or their compiler emits warnings about variable fall-through and they've decided to just use braces all the time. Personally, I think it's cleaner to only use them when necessary.

OTHER TIPS

The event that comes back from event.getAction() is actually an integer. It contains more than just information about the type of action apparently. It probably contains flags that describe the action in more detail. And by passing all that detail in through an int, it saves the use of a class object to represent the action itself.

The mask is bitwise for the first byte in the integer. Only the first two bytes out of the eight. And for comparison purposes, you need to remove the upper parts of the integer before comparing it against the actions, which are themselves only using the bottom byte of an integer.

The upper parts of the event integer are still useful for other purposes when checking against them. So the mask is 0x000000ff representing that you only want information related to the action type at the bottom and the entire event takes up the rest of it with other useful information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top