16

What is the difference between the two approaches below?

 int action1 = event.getAction() & MotionEvent.ACTION_MASK;

 int action2 = event.getAction();
iCould7
  • 635
  • 6
  • 10
  • Note: `event.getAction() & MotionEvent.ACTION_MASK` is the same as `getActionMasked()`. See [this question](https://stackoverflow.com/q/17384983/3681880), also. – Suragch Jul 28 '17 at 01:23

1 Answers1

17

The ACTION_MASK is used to separate the actual action and the pointer identifier (e.g. first finger touched, second finger touched, etc.) The first 8 bits of the value returned in getAction() is the actual action part, and so when you bitwise-AND it with the action mask (= 11111111 = 255 = 0xff), you are left with only the action and none of the pointer information.

Keep in mind here that & is used as an arithmetic operator (bitwise) and not a logical operator (single & is a perfectly valid logical operator in Java, as is &&).`

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195