由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - use >>>= with short
相关主题
InputStream.read() 被block的问题(陷入无限等待)xp 是不是不支持jave 阿?
奇怪的Eclipse问题Help!! a cookie question
trucated class fileAsk a simple question about throw exception, bow bow bow
local变量被赋值了几次?为什么java没有destructor
请教一题Jave and Jboss
问一个java基础的初始化的问题,一直搞不明白 (转载)一个JAVA系统的面试问题
Core Java2 Notes (4)jboss and jave
question about JbuilderPython vs J2EE
相关话题的讨论汇总
话题: short话题: int话题: right话题: 10
进入Java版参与讨论
1 (共1页)
r********r
发帖数: 208
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."
r********r
发帖数: 208
2
刚查了Integer.toBinaryString(),是它把s由short类型promote/cast为int了。
f*******n
发帖数: 12623
3
Integer.toBinaryString()本身就take int。所以你call的时候就变成int给它
c*********e
发帖数: 16335
4
re

【在 f*******n 的大作中提到】
: Integer.toBinaryString()本身就take int。所以你call的时候就变成int给它
1 (共1页)
进入Java版参与讨论
相关主题
Python vs J2EE请教一题
Re: Jave, C++, or C#? (转载)问一个java基础的初始化的问题,一直搞不明白 (转载)
c++熟手如何学习Java直到能够参与开发企业级应用?Core Java2 Notes (4)
对J2EE的几个迷惑 (转载)question about Jbuilder
InputStream.read() 被block的问题(陷入无限等待)xp 是不是不支持jave 阿?
奇怪的Eclipse问题Help!! a cookie question
trucated class fileAsk a simple question about throw exception, bow bow bow
local变量被赋值了几次?为什么java没有destructor
相关话题的讨论汇总
话题: short话题: int话题: right话题: 10