tags:

views:

99

answers:

4

Fallowing statement is from Character class of java:

(1 << Character.PARAGRAPH_SEPARATOR)) >> 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.

+2  A: 

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

Isaac
+4  A: 

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.

Marcelo Cantos
axtavt
@axtavt: Thanks for the explanation.
Marcelo Cantos
@Marcelo Cantos and @actavt , Thanks alot :)
feridcelik
+1  A: 

<< is Left Shift: 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 waaay 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.

Leo Izen
A: 

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.

steinar