I'm in the middle of studying the book named Beginning Android Games. One thing that I noticed was this:
int action = event.getAction() & MotionEvent.ACTION_MASK;
int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK)
>> MotionEvent.ACTION_POINTER_ID_SHIFT;
This is the firsr time I've seen a variables like that so I don't know what it does. I ran the code in java and created some sample.
If I run this code:
int i = 10 >> 500;
System.out.print("Answer " + i);
The answer would be 0? Why is that?
And if I run this code:
int i = 10 & 500;
System.out.print("Answer " + i);
At first I thought it was concatenation of value so I would assume that i = 10500 but thats not the case. The answer is the same. still 0? Anyone knows what's happening here?
& is bitwise operator whereas && is conditional operator.You are playing with bitwise operator.
The AND operator specifies that both Signals A and B must be charged for the result to be charged. Therefore, AND-ing the bytes 10 and 6 results in 2, as follows:
See here for more examples:
Bitwise and Bit Shift Operators
In order to understand bitwise operators it's not very helpful to use base 10 numbers, but better to use base 2 (binary).
So in your first example you're doing taking the number 0b111110100 and shifting it 500 places to the right. which will leave you with 0.
If instead you had shifted it right by one place, you would get 0b11111010 or 250. Or for two places 125.
In your second example you are taking the numbers 0b111110100 and %1010 and "anding" them together. This gives 0, because the and operator only returns 1 for positions that both have 1, and in your case, none do. For a more illustrative example try 18 & 2. This gives 2. Why? Well because 0b10010 and 0b10 only have one 1 in common, so that's the result.
I think you can first read some basics of operators in java.
>>
this is not concatenation operator its shift operator in java.&
is called bitwise ANDThey are called Bitwise Operators.
1.
is signed right shift of bit patterns of 10 by 500. 10 can be represented as 1010(ignoring the sign bit in this case) in binary number system. Right shifting each bit here by 500, will result the final number to be 0.
2.
is bit by bit AND of bit pattern of 10 and 500.10 can be represented as 1010 and 500 as 1 1111 0100(ignoring the sign bit in this case) in binary number system. Bitwise AND of each bit of 0 0000 1010 with 1 1111 0100 results in 0.
You can use
System.out.println(Integer.toBinaryString(number));
to print the bit pattern and check.