Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does << mean in Java?

Tags:

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); } 

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

I would really appreciate someone telling me, thanks!

like image 248
Ants Avatar asked Jan 25 '10 10:01

Ants


People also ask

What do ++ and — mean in Java?

In java, ++ and — signs represent increment and decrement operators, respectively. The ++ and — operators respectively increase and decrease the variable’s value by 1. Both these operators can be used as either prefix or postfix.

What is the use of += in Java?

+= is compound addition assignment operator which adds value of right operand to variable and assign the result to variable. Types of two operands determine the behavior of += in java. In the case of number, += is used for addition and concatenation is done in case of String.

What is the use of operator in Java?

Java Operators. Operators are used to perform operations on variables and values. In the example below, we use the + operator to add together two values:

What is the behavior of += in Java?

Types of two operands determine the behavior of += in java. In the case of number, += is used for addition and concatenation is done in case of String. a+=b is similar to a=a+b with one difference which we will discuss later in the article.


2 Answers

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

like image 178
Itay Maman Avatar answered Sep 22 '22 00:09

Itay Maman


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.

like image 26
Bozho Avatar answered Sep 23 '22 00:09

Bozho