2

Possible Duplicate:
What does >> do in java?

What does it do this sign: << ? This is in Java.

For an instance, new CustomPermission(1 << 5, 'M');

Best regards

Community
  • 1
  • 1
ucas
  • 417
  • 1
  • 11
  • 30
  • @sepp2k. The answer over there answers this question too: http://stackoverflow.com/questions/3921145/what-does-do-in-java/3921168#3921168 so yes, it is a duplicate. – TRiG Aug 08 '11 at 09:46

5 Answers5

4

It's bitshift

http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

Basically it multiplies the left operand by 2 to the power of the right operand

so 1 << 5 is 1 * 2^5

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
1

its a bit shift operator

this instruction shifts 00000001 five bits to the left: so it becomes 00100000

Wizz
  • 1,267
  • 1
  • 13
  • 20
1

It's a signed left shift. What you're calculating there is basically 2^5, as you are shifting 00000001 5 times left, which results in 00100000

Tedil
  • 1,935
  • 28
  • 32
1

The '<<' operator shifts all the bits of the number on the left by the value on the right.

For example 1<<5 will be equal to 32.
This is often used in place of multiplication by a power of 2 as in 1<<5 is equivalent to 1*(2^5) or 1*32

Shadow
  • 6,161
  • 3
  • 20
  • 14
1

In addition to the other answers, I will also add that bit shifting is commonly used in code which uses bitmasks. So bit 5 in the given code fragment has a specific meaning, which is different to, say bit 3. Using the '<<' operator you can logically combine the different bits to give a combined bitmask.

Andrew Fielden
  • 3,751
  • 3
  • 31
  • 47