2

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?

CodeAndWave
  • 1,586
  • 3
  • 23
  • 33
  • 3
    You're working with bits, not with plain integers. There are tutorials on the net about this, like [Bitwise and Bit Shift Operators](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html) and http://stackoverflow.com/q/6250114/1065197 – Luiggi Mendoza Apr 23 '13 at 06:20
  • is there a reason why i got downvote? – CodeAndWave Apr 23 '13 at 06:31
  • None that I can understand... Anyway, don't think about the downvotes, focus on the task at hand: learning about bit operations :). – Luiggi Mendoza Apr 23 '13 at 06:32

3 Answers3

11

& 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:

a = 0000 1010 (10)

b = 0000 0110 (6)
  ---- ----
r = 0000 0010 (2)  // a&b

See here for more examples:

Bitwise and Bit Shift Operators

~       Unary bitwise complement
<<      Signed left shift
>>      Signed right shift
>>>     Unsigned right shift
&       Bitwise AND
^       Bitwise exclusive OR
|       Bitwise inclusive OR
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
3

They are called Bitwise Operators.

1.

int i = 10 >> 500;

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.

int i = 10 & 500;

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.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • 1
    I am not here to compete with any answers. I just put pointers so that OP can work in the correct direction and solve the problems himself ;) – AllTooSir Apr 23 '13 at 06:40
  • 1
    I'm not here to compete with answers either, just to make sure that if somebody post an answer, he/she does the job **the best it can be done**. By the way, it's not me who dictates this, I back my opinion on answers based on Meta: [What is an acceptable answer?](http://meta.stackexchange.com/a/118694/182862) and your first attempt is covered on section 12. – Luiggi Mendoza Apr 23 '13 at 06:42
2

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.

Mikkel Løkke
  • 3,710
  • 23
  • 37