对Hadoop中一段将字节码数组转换为Integer的代码的思考

在Hadoop的类org.apache.hadoop.io.WritableComparator中有如下一段代码,完成将Integer值序列化后对应的字节码数组重新反序列化为Integer的功能。

[java]  view plain copy
  1. /** Parse an integer from a byte array. */  
  2. public static int readInt(byte[] bytes, int start) {  
  3.   return (((bytes[start  ] & 0xff) << 24) +  
  4.           ((bytes[start+1] & 0xff) << 16) +  
  5.           ((bytes[start+2] & 0xff) <<  8) +  
  6.           ((bytes[start+3] & 0xff)));  
  7.   
  8. }  

 

初读这一段代码我怎么也没有想明白,其中究竟是如何实现的了。

为了搞清楚其中的究竟,我写了如下一个测试类,试图理解其中的原理,谁知到越搞觉得越是不明白了,甚至我都怀疑起来我原来理解的&操作和<< 、>>等操作是否正确了。

[java]  view plain copy
  1. public class TestMain  
  2. {  
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args)  
  7.     {  
  8.         System.out.println("Binary  5:" + Integer.toBinaryString(-5));  
  9.         System.out.println("Binary -5:" + Integer.toBinaryString(5));  
  10.           
  11.         byte b = new Integer(-5).byteValue();  
  12.         System.out.println("byte -5:" + b);  
  13.         System.out.println("-5 & 0xff:" + (b & 0xff));  
  14.           
  15.           
  16.         System.out.println("Binary  -1:" + Integer.toBinaryString(-1));  
  17.         System.out.println("Binary  251:" + Integer.toBinaryString(251));  
  18.         System.out.println("Binary  256:" + Integer.toBinaryString(256));  
  19.           
  20.         System.out.println("Hex  256:" + Integer.toHexString(256));  
  21.           
  22.         System.out.println("Binary  Integer.MAX_VALUE:" + Integer.toBinaryString(Integer.MAX_VALUE));  
  23.         System.out.println("Hex     Integer.MAX_VALUE:" + Integer.toHexString(Integer.MAX_VALUE));  
  24.     }  
  25. }  
  26. public class TestMain  
  27. {  
  28.     /** 
  29.      * @param args 
  30.      */  
  31.     public static void main(String[] args)  
  32.     {  
  33.         System.out.println("Binary  5:" + Integer.toBinaryString(-5));  
  34.         System.out.println("Binary -5:" + Integer.toBinaryString(5));  
  35.           
  36.         byte b = new Integer(-5).byteValue();  
  37.         System.out.println("byte -5:" + b);  
  38.         System.out.println("-5 & 0xff:" + (b & 0xff));  
  39.           
  40.           
  41.         System.out.println("Binary  -1:" + Integer.toBinaryString(-1));  
  42.         System.out.println("Binary  251:" + Integer.toBinaryString(251));  
  43.         System.out.println("Binary  256:" + Integer.toBinaryString(256));  
  44.           
  45.         System.out.println("Hex  256:" + Integer.toHexString(256));  
  46.           
  47.         System.out.println("Binary  Integer.MAX_VALUE:" + Integer.toBinaryString(Integer.MAX_VALUE));  
  48.         System.out.println("Hex     Integer.MAX_VALUE:" + Integer.toHexString(Integer.MAX_VALUE));  
  49.     }  
  50. }  

 

上述程序输出为:

Binary  5:11111111111111111111111111111011
Binary -5:101
byte -5:-5
-5 & 0xff:251
Binary  -1:11111111111111111111111111111111
Binary  251:11111011
Binary  256:100000000
Hex  256:100
Binary  Integer.MAX_VALUE:1111111111111111111111111111111
Hex     Integer.MAX_VALUE:7fffffff

 

我一直在琢磨,正数和0xff取与也就巴了,但是负数怎么办呢?

后来我决定先看看我头脑中理解的<<操作是左移位,>>是右移位,0xff表示0111 1111是否正确,在网上找了一下,确实没有错啊,就是我理解的那样啊,这样看来就是我们那个正数与负数的字节码表示的玄机在中间起到作用了。

 

结果在网上一搜索,找到如下一篇文章,算是彻底的让我明白了为什么啦。

汗颜不已,基础知识。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值