码迷,mamicode.com
首页 > 编程语言 > 详细

Java里数值类型转换

时间:2017-07-09 23:53:14      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:数值   import   out   end   ber   bin   推断   amp   java   

 1 import static java.lang.System.out;
 2 
 3 public class testOnNumber {
 4     public static void main(String[] args){
 5         //int转换成short,把低位截断给short
 6         int i = 0xa0001231;
 7         out.println(Integer.toBinaryString(i));
 8         out.println(i);
 9         short s = (short) i;
10         out.println(s);
11 
12         //mask的效果差不多是把short转成unsigned,估计是因为s2 & mask的结果是int,而符号位为0
13         out.println();
14         short s2 = (short) 0x9004;
15         int mask = 0xffff;
16         out.println(Integer.toBinaryString(s2));
17         out.println(Short.toUnsignedInt(s2));
18         out.println(s2 & mask);
19         out.println(s2);
20 
21         //可以据此推断出Java里数据存为二进制的方式是big-Endian?
22         out.println();
23         int i2 = 0x732132ab;
24         out.println(Integer.toBinaryString(i2));
25         short s3 = (short) i2;
26         short s4 = (short) (i2 >> 16);
27         out.println(Integer.toBinaryString(s3));
28         out.println(Integer.toBinaryString(s4));
29     }
30 }

输出结果:

10100000000000000001001000110001
-1610608079
4657

11111111111111111001000000000100
36868
36868
-28668

1110011001000010011001010101011
11001010101011
111001100100001

Java里数值类型转换

标签:数值   import   out   end   ber   bin   推断   amp   java   

原文地址:http://www.cnblogs.com/ITStruggler/p/7143594.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!