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!
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.
+= 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.
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:
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With