用Java长时间移位

编程入门 行业动态 更新时间:2024-10-11 21:32:16
本文介绍了用Java长时间移位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于任何人来说,这都是一件容易的事!

Im sure this is an easy one for whoever sees it first!

为什么在Java中代码类似

Why in Java does code like

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));

打印

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

这对我来说没有意义. long是一个64位数字-而在上面它的作用类似于int.我知道移位的byte会进行int提升,但是在这种情况下我看不到发生了什么.

This does not make sense to me. long is a 64 bit number - whereas it seems to act like an int in the above. I know bitshifted bytes undergo int promotion but I dont see whats going on in this case.

任何有关这里发生的事情的指针都将是不错的:)

Any pointers on whats going on here would be good :)

Thx

编辑:感谢您提供所有答案-单击提交"后,我意识到发生了什么,但是我进入了readonly模式,因此无法删除!非常感谢!

EDIT: thanks for all the answers - i realised what was going on as soon as I clicked 'submit' but SO went into readonly mode and I couldnt delete! Many thanks!

推荐答案

这是因为1是int文字,因此<<应用于整数.结果被强制转换为long,但那时为时已晚.

It's because 1 is an int literal, so << is applied to an integer. The result is cast to a long, but by then it's too late.

如果您写1L<< 32,等,那么一切都会好起来的. L用于表示long文字.

If you write 1L << 32, etc., then all will be well. L is used to denote a long literal.

更多推荐

用Java长时间移位

本文发布于:2023-11-05 03:12:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559748.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:长时间   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!