What are bitwise operations?

2020-01-29 01:51发布

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?

4条回答
劫难
2楼-- · 2020-01-29 02:22

& 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
查看更多
闹够了就滚
3楼-- · 2020-01-29 02:28

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.

查看更多
家丑人穷心不美
4楼-- · 2020-01-29 02:31

I think you can first read some basics of operators in java.

>> this is not concatenation operator its shift operator in java.

The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand.

& is called bitwise AND

The bitwise & operator performs a bitwise AND operation.

查看更多
叼着烟拽天下
5楼-- · 2020-01-29 02:42

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.

查看更多
登录 后发表回答