avatar
use >>>= with short# Java - 爪哇娇娃
r*r
1
在下面的程序中,对short类型s,无符号右移并赋值(见最后引用,这个操作中间经历
int, right shift, truncation 3个过程)。难道结果不应该还是short类型吗?可是
结果-1是int,32位.没想明白,很不爽。谁能解惑,指点迷津?
//: operators/URShift.java
// Test of unsigned right shift.
//import static net.mindview.util.Print.*;
public class URShift {
public static void main(String[] args) {
short s = -1;
System.out.println(Integer.toBinaryString(s));
System.out.println(Integer.toBinaryString(s>>>10) + ": s>>>10");
s >>>= 10;
System.out.println(Integer.toBinaryString(s));
}
} /* Output:
11111111111111111111111111111111
1111111111111111111111: s>>>10
11111111111111111111111111111111
*///:~
---------The following are quoted from <>-----
"If you use it with byte or short, you don't get the correct results.
Instead, these are promoted to int and right shifted, but then truncated as
they are assigned back into their variables, so you get -1 in those cases."
avatar
r*r
2
刚查了Integer.toBinaryString(),是它把s由short类型promote/cast为int了。
avatar
f*n
3
Integer.toBinaryString()本身就take int。所以你call的时候就变成int给它
avatar
c*e
4
re

【在 f*******n 的大作中提到】
: Integer.toBinaryString()本身就take int。所以你call的时候就变成int给它
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。