Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java << Operator [duplicate]

Tags:

java

operators

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!

like image 715
PaulG Avatar asked Nov 28 '22 09:11

PaulG


2 Answers

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.

like image 58
Jon Skeet Avatar answered Dec 05 '22 07:12

Jon Skeet


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.

like image 41
dlev Avatar answered Dec 05 '22 05:12

dlev