-6

Can anyone tell me what this is doing:

    System.out.println(1 << val);

for val = 17, the answer is coming as: 131072

2 Answers2

1

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

This is a bitwise operation. Basically you are shifting the bits that originally made up your variable, and then those bits now represent a new number.

In this case, you shift to the left a few spots, which suddenly adds a lot more bits into your number... adding a greater value to it.

How do shift operators work in Java?

Community
  • 1
  • 1
SnakeDoc
  • 13,611
  • 17
  • 65
  • 97
1

Binary representation of 1 is 00000000000000000000000000000001

1 << 17 will move the last 1 in the binary representation 17 places left, which will result in 0000000000000100000000000000000, which when converted back to decimal results is 131072

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162