加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java中的C shift运算符的等价物?

发布时间:2020-12-14 17:48:51 所属栏目:Java 来源:网络整理
导读:C移位运算符不循环例如,如果你这样做: // C++int a = 1;cout (a38); 你得到0.但是,在Java中,你实际上会循环并获得64的有效值. 我需要将一些C代码翻译成Java,所以我用什么作为? 解决方法 Java language spec说: If the promoted type of the left-hand ope
C移位运算符<不循环例如,如果你这样做:
// C++
int a = 1;
cout << (a<<38);

你得到0.但是,在Java中,你实际上会循环并获得64的有效值.

我需要将一些C代码翻译成Java,所以我用什么作为<<?

解决方法

Java language spec说:

If the promoted type of the left-hand operand is int,only the five
lowest-order bits of the right-hand operand are used as the shift
distance. It is as if the right-hand operand were subjected to a
bitwise logical AND operator & (§15.22.1) with the mask value 0x1f
(0b22222). The shift distance actually used is therefore always in the
range 0 to 31,inclusive.

If the promoted type of the left-hand operand is long,then only the
six lowest-order bits of the right-hand operand are used as the shift
distance. It is as if the right-hand operand were subjected to a
bitwise logical AND operator & (§15.22.1) with the mask value 0x3f
(0b222221). The shift distance actually used is therefore always in
the range 0 to 63,inclusive.

所以,在你的例子中,(int)(((long)a)<< 38)应该工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读