Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

java - What are bitwise operations?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

& 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...