tags:

views:

453

answers:

8

Hi, I can't find out what << means in java, because I can't search for it on Google I am absolutely lost!

The code in question is:

public int getRGB() {
    return ((red << 16) | (green << 8) | blue);
}

taken from http://java.sun.com/docs/books/tutorial/essential/concurrency/example/ImmutableRGB.java

Would really appreciate someone telling me, thanks!

+13  A: 

Left shift of the bits

If red == 4 (which in binary is: 00000100) then red << 16 will insert sixteen 0-bits at its right, yielding: 000001000000000000000000 which is 262144 in decimal

Itay
Legend! Thanks a lot.
Ants
You're right. Thanks to @Aamir for fixing it
Itay
+1  A: 

Its a left bit shift

Neeraj
+4  A: 

Left shift a number of bits. It is equivalent to multiplying by two that many times.

It's used for setting specific bits in a byte, or specific bytes in a word.

Milan Ramaiya
+13  A: 

Q. What is this?
A. An "operator"

Q. How do I get to know about operators in java?
A. Google for "Java operators"

And the result is this:

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.

Bozho
Kudos for teaching him how to fish instead of giving the man a fish.
Sindri Traustason
+1  A: 

Its left shifting and convert Red, Green, Blue into 24 bit Number

S.Mark
+1  A: 

It's the left-shift operator. If you google for those words, you'll get links like this one, explaining how to use it.

Blorgbeard
+2  A: 

its a bit shift. search for operators java, it will return you detailed explanations.

bertolami
+1  A: 

It is the left shift operator. Here's some more information on shift operators from the Java Tutorial.

In your example code the three integer values: red, green and blue will typically have values 0-255. Hence, it is possible to combine these values and represent them as a single integer by shifting the red value by 16 bits, shifting the green value by 8 bits and then performing a bitwise-OR operation to combine the values.

Adamski