5

Possible Duplicate:
What is the following sign: <<?

Can someone please explain the << in the follow sample code?

final static public int MY_VAR = 1<<3;

Thank you!

Community
  • 1
  • 1
PaulG
  • 466
  • 1
  • 6
  • 19

6 Answers6

13

Sure, it's a left shift - you're shifting the number "1" left by 3 bits, so the result will be 8.

See section 15.19 of the Java Language Specification for more details.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
9

It's the bitwise left-shift operator. It shifts bits to the left, like so:

00000001 << 3 == 00001000

In other words, 1 << 3 == 8, since you shift the 1 bit over by 3 places.

dlev
  • 48,024
  • 5
  • 125
  • 132
3

It is a bitshift operator. See http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html for docs.

lucapette
  • 20,564
  • 6
  • 65
  • 59
3

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.

[Source]

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
2

<< the left shift operator.

Basically it moves every bit in the left-hand value to the left by an amount indicated by the right-hand value.

So 0b1 (decimal 1) becomes 0b1000 (decimal 8) in this example.

It's explained in this tutorial and illustrated in this Wikipedia article.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
0

It's 2 raised to the power of 3. It's a bitwise operator SHIFT_LEFT: 0001 => 0100

Rekin
  • 9,731
  • 2
  • 24
  • 38