Java里数值类型转换
Posted TooLateToLearn!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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里数值类型转换的主要内容,如果未能解决你的问题,请参考以下文章