In the expression return (1 << 64) - 1
, the <<
operator is a left shift operator used for bit manipulation. It shifts the bits of the number 1
to the left by 64
positions.
When we shift the bits of 1
to the left by 64
positions, we get a number that has 1
at the 65th bit and 0
in all other positions. This is because 1
in binary is represented as 000...001
, and shifting it to the left by 64
positions adds 64
zeros to the right.
Therefore, (1 << 64)
results in the number 2^64
or 18446744073709551616
in decimal.
By subtracting 1
from this value, we effectively get the total number of grains for all squares of a chessboard. This is because in a chessboard, the number of grains on each square doubles compared to the previous square. So, if we subtract 1
from 2^64
, we are effectively subtracting 1
grain from the total, which accounts for the initial square.
To understand it better, imagine the following:
1
grain.2
grains.4
grains.In total, if we sum the number of grains on each square, we get 2^64 - 1
.
Therefore, (1 << 64) - 1
is used in this context to represent the total number of grains for all squares of a chessboard.
gistlibby LogSnag