开发者

"<<" operator in java

Fallowing statement is from Character class of java:

(1 << Character.PARAGRAPH_SEPARATOR)) &开发者_C百科gt;> type

PARAGRAPH_SEPARATOR is a byte and type is an integer.

The operators in this sentence, what do they do? how and where I can use those operators?

Here is the oracles java.lang.Character doc. Nearly all the methods in the class uses those operators.


They are bit-shift operators. << shifts the bits "left" (towards the most-significant bit), and vice-versa for >>. Shifting left or right by n bits is pretty much the same as multiplying or dividing, respectively, by 2n.

See @axtavt's comment for an explanation of how these operators are being used in this context.


These are the bitwise shift operators.

If you left shift the following byte:

00000001

you would get:

00000010

I.e. the pattern has "shifted" to the left and zeros fill in on the right. So if you apply the right shift operator >> on that result, you'll get the original byte again.

You'll notice that the decimal values of these numbers are 1 and 2. If you shift left once again you'll get:

00000100 = 4

So you see that shifting to the left multiplies the number by two (given that it doesn't overflow), while right shifting divides by two. This happens very efficiently in most computers. So that's one example of how you might use these operators in a practical way.


Bitwise operators: http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html


<< is the left shift operator: It shifts the binary number stored in the computer left. For example, 9 in binary is 1001. 9 << 2 makes 100100 in binary (36), because it shifts it left and adds 0s at the end. 1 << n is the same thing as Math.pow(2, n) except it is way faster and better in general, as well as returning int, not double.

>> is right shift. It shifts it right, and discards empty bits. 13 is 1101 in binary, so 13 >> 1 is 110 in binary, or 6 normally.


It works as a highly optimized multiple comparison. 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. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

for more detail,You can visit below link . http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜