Easy in Java

Make sure it's an easy person who sees it first!

Why code in Java

long one = 1 << 0;
   long thirty = 1 << 30;
   long thirtyOne = 1 << 31;
   long thirtyTwo = 1 << 32;

   System.out.println(one+" = "+Long.toBinaryString(1 << 0));
   System.out.println(thirty+" = "+Long.toBinaryString(1 << 30));
   System.out.println(thirtyOne+" = "+Long.toBinaryString(1 << 31));
   System.out.println(thirtyTwo+" = "+Long.toBinaryString(1 << 32));

Print

1 = 1
1073741824 = 1000000000000000000000000000000
-2147483648 = 1111111111111111111111111111111110000000000000000000000000000000
1 = 1

It means nothing to me Long is a 64 - bit number, and it looks like int above I know bithifted bytes are int upgraded, but I don't know what happens in this case

Any pointer to what's happening here will be good:)

thank you

Editor: thanks for all the answers - I realize what happened. Once I click "submit", but enter read-only mode, I can't delete it! Thank you.

Solution

This is because 1 is an int literal, so < is applied to an integer The result was long, but by then it was too late If you write 1L < 32 and so on, everything will be fine L is used to represent a long literal value

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>