First of all, your code doesn't make a lot of sense. If you shift a long
variable 64 places to the left, you should end up with zero, since you've shifted away all of the long
's original bits.
Second, as you've correctly mentioned, Java treats all int
s and long
s as singed (as stated here). So it considers a 64 bit shift on a long
to be equal to a 0 shift. That's why instead of getting zero, you're left with the original value, which is 1. If you want to initialize a
to 1
followed by 63 zeroes I suggest avoiding shifts, and just using something like this:
long a = 0x8000_0000_0000_0000L;
It's readable by anyone who understands binary, and there's not Java bit shift magic involved.
If you want to keep using shift, just use a 63 shift instead:
long a|=1L<<63;