Itβs important to mark the line here
System.out.print((long)(1 << (63)));
First you take (1 << 63)
, and then you throw to the end. As a result, you actually shift to the left in integers, so a long throw has no effect. Therefore, if you shift 63 bits to the left, the value of min will be minimal, not long.
But there is another, more important point. Java longs are always signed, so even a string
System.out.print(1L << 63);
will give a negative number. Under two additions, when the leftmost bit is 1, the number is negative.
In fact, you cannot imagine the number 2 63 = 9223372036854775808 in the Java primitive type, because this number is larger than the maximum, and long
is the largest primitive type. You can represent this number as BigInteger . You can even generate it with a left shift of 63 with the code
BigInteger.ONE.shiftLeft(63)
source share