byte取高4位,低4位,byte转int

byte abyte =-1;
    System.out.println(abyte);
    System.out.println(Integer.toBinaryString(abyte));
    //取高四位
    byte high = (byte) ((abyte>>4) & 0x0f);
    System.out.println("取高四位"+Integer.toBinaryString(high));
    //取低四位
    byte low =  (byte) (abyte & 0x0f);
    System.out.println("取低四位"+Integer.toBinaryString(low));
    //byte转int保持数值不变
    int b= (int)abyte;
    System.out.println(b);
    //byte转int保持最低字节中各个位不变
    int c= (int)(abyte & 0xff);
    System.out.println(c);

 

-1
11111111111111111111111111111111
取高四位1111
取低四位1111
-1
255

posted @ 2017-08-18 14:48  tonggc1668  阅读(883)  评论(0编辑  收藏  举报