当前位置: 技术问答>java相关
基础性问题,有兴趣的来看看
来源: 互联网 发布时间:2015-02-07
本文导语: >>算术右移,int型数只使用低5位,long只使用低6位 (thinking in java和认证考试指南都这么写) 理解不了,哪位能给我讲讲 | 是这个意思: X >>> Y 当X为int时,Y不得大于32;当X为long时,Y不得...
>>算术右移,int型数只使用低5位,long只使用低6位
(thinking in java和认证考试指南都这么写)
理解不了,哪位能给我讲讲
(thinking in java和认证考试指南都这么写)
理解不了,哪位能给我讲讲
|
是这个意思:
X >>> Y
当X为int时,Y不得大于32;当X为long时,Y不得大于64;
如果Y大于32或64,则X只移动32位或64位,因为不管再移动多少位,结果都是一样为0。
这主要是防止程序员写错程序,Y太大,导致cpu处理时间过长。所以执行上面的语句时,
只用Y的低5位(最大32)或低6位(最大64)。
明白了吗?
见java2认证学习指南(英文原版)第37、38页。
X >>> Y
当X为int时,Y不得大于32;当X为long时,Y不得大于64;
如果Y大于32或64,则X只移动32位或64位,因为不管再移动多少位,结果都是一样为0。
这主要是防止程序员写错程序,Y太大,导致cpu处理时间过长。所以执行上面的语句时,
只用Y的低5位(最大32)或低6位(最大64)。
明白了吗?
见java2认证学习指南(英文原版)第37、38页。
|
他们都是从这里(JLS)来的:
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5121
。。。
The type of each of the operands of a shift operator must be a primitive integral type, or a compile-time error occurs. Binary numeric promotion (§5.6.2) is not performed on the operands; rather, unary numeric promotion (§5.6.1) is performed on each operand separately. The type of the shift expression is the promoted type of the left-hand operand.
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. 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. The shift distance actually used is therefore always in the range 0 to 63, inclusive.
。。。
我也没看懂为什么要用一个5位/6位的mask,哈哈哈哈哈
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5121
。。。
The type of each of the operands of a shift operator must be a primitive integral type, or a compile-time error occurs. Binary numeric promotion (§5.6.2) is not performed on the operands; rather, unary numeric promotion (§5.6.1) is performed on each operand separately. The type of the shift expression is the promoted type of the left-hand operand.
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. 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. The shift distance actually used is therefore always in the range 0 to 63, inclusive.
。。。
我也没看懂为什么要用一个5位/6位的mask,哈哈哈哈哈
|
Y是可以大于32,但是,大于32的那部分最后被扔掉了。
33:100001
移位时,只用了低5位,所以是00001,即1,所以只移1位。
33:100001
移位时,只用了低5位,所以是00001,即1,所以只移1位。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。