Flavian Hautbois
Flavian Hautbois

Reputation: 3070

Java syntax >>, &, ? and :

I don't understand what the >>, &, ? and : mean in this program:

case MotionEvent.ACTION_POINTER_UP: {
        // Extract the index of the pointer that left the touch sensor
        final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) 
                >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        final int pointerId = ev.getPointerId(pointerIndex);
        if (pointerId == mActivePointerId) {
            // This was our active pointer going up. Choose a new
            // active pointer and adjust accordingly.
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mLastTouchX = ev.getX(newPointerIndex);
            mLastTouchY = ev.getY(newPointerIndex);
            mActivePointerId = ev.getPointerId(newPointerIndex);
        }
        break;

Could you help me ? Google doesn't do search on non-alphanumeric characters...

Upvotes: 4

Views: 15167

Answers (4)

Shraddha
Shraddha

Reputation: 2335

>> is the shift operator and it shifts a bit pattern to the right

(You can google shift operator)

& is a bitwise and operator (search bitwise operators)

? : is short form for if - then -else [if cond] ? [then] : [else]

Refer http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

Upvotes: 4

Todd
Todd

Reputation: 31720

Let's take these one at a time...

final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) 
            >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;

These are bitwise operators. The & is ANDing the bits in action and MotionEvent.ACTION_POINTER_INDEX_MASK together.

The >> is shifting that result.

Binyamin referenced a good page on bitwise operators for you.

final int newPointerIndex = pointerIndex == 0 ? 1 : 0;

This is a ternary operator. Essentially it's saying "if pointerIndex is equal to 0, return 1, else return 0. The way you express this is (boolean decision) ? valueIfTrue : valueIfFalse

Upvotes: 5

Kowser
Kowser

Reputation: 8261

  • Here >> is shift operator.
  • ? : is Ternary Operator
  • & is Bitwise and operator

Take a look here for tutorial

Upvotes: 1

user142162
user142162

Reputation:

Upvotes: 8

Related Questions