In the Core Java Volume1 book there is a caution that say:
CAUTION: The right-hand side argument of the shift operators is reduced modulo 32
(unless the left-hand side is a long, in which case the right-hand side is reduced modulo 64).
For example, the value of 1 << 35 is the same as 1 << 3 or 8.
What exactly does this mean? also why 1 become 8, instead of being 0 after 35 left shifting?
many thanks
In many programming languages, shifting by more than the size of a numeric data type (32 bits for an int, 64 bits for a long) is undefined. On the other hand, Java defines it such that
(n << d)is equivalent to(n << (d % 32))wherenis an int, and(n << d)is equivalent to(n << (d % 64))wherenis a long.So,
1 << 35is equivalent to1 << (35 % 32), which equals1 << 3 = 8.