Can anyone tell me what this is doing:
System.out.println(1 << val);
for val = 17, the answer is coming as: 131072
Can anyone tell me what this is doing:
System.out.println(1 << val);
for val = 17, the answer is coming as: 131072
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.
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