1. 程式人生 > >如何獲取byte的各個bit值以及常見位操作

如何獲取byte的各個bit值以及常見位操作

專案中通過訊號採集板的資料獲取車上儀表盤指示燈的資訊,將接收到的資料轉成byte後,還要將每一個Byte的各個Bit值分離出來,這樣才知道每個bit的值代表的具體資訊。這裡記錄下如何獲取byte的各個bit值,一些常見的位操作也順便記錄下。

1、分離出一個Byte的各個Bit的值

一個英文字元佔一個位元組(1字母=1 byte=8 bit),一個漢字佔兩個位元組(1漢字=2 byte=16 bit)。

其中,bit: 位,一個二進位制資料0或1,是1bit。

     byte:位元組,儲存空間的基本計量單位,1 byte=8 bit。

byte:1個位元組(8位) (-128~127)  (-2^7~2^7-1)

short:2個位元組(16位) (-32768~32767)  (-2^15~2^15-1)

int:4個位元組(32位) (-2147483648~2147483647)  (-2^31~2^31-1)

long:8個位元組(64位) (9223372036854774808~9223372036854774807)  (-2^63~2^63-1)

float:4個位元組(32位) (3.402823e+38~1.401298e-45)  (e+是乘以10的38次方,e-45是乘以10 的負45次方)

double:8個位元組(64位) (1.797693e~4.9000000e-324)

(1)byte-->bit

複製程式碼
public class T {  
    /** 
     * 將byte轉換為一個長度為8的byte陣列,陣列每個值代表bit 
     */  
    public static byte[] getBooleanArray(byte b) {  
        byte[] array = new byte[8];  
        for (int i = 7; i >= 0; i--) {  
            array[i] = (byte)(b & 1);  
            b = (byte) (b >> 1);  
        }  
        
return array; } /** * 把byte轉為字串的bit */ public static String byteToBit(byte b) { return "" + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1) + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1) + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1) + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1); } public static void main(String[] args) { byte b = 0x35; // 0011 0101 // 輸出 [0, 0, 1, 1, 0, 1, 0, 1] System.out.println(Arrays.toString(getBooleanArray(b))); // 輸出 00110101 System.out.println(byteToBit(b)); // JDK自帶的方法,會忽略前面的 0 System.out.println(Integer.toBinaryString(0x35)); } }
複製程式碼

(2)bit-->byte

複製程式碼
/** 
 * 二進位制字串轉byte 
 */  
public static byte decodeBinaryString(String byteStr) {  
    int re, len;  
    if (null == byteStr) {  
        return 0;  
    }  
    len = byteStr.length();  
    if (len != 4 && len != 8) {  
        return 0;  
    }  
    if (len == 8) {// 8 bit處理  
        if (byteStr.charAt(0) == '0') {// 正數  
            re = Integer.parseInt(byteStr, 2);  
        } else {// 負數  
            re = Integer.parseInt(byteStr, 2) - 256;  
        }  
    } else {// 4 bit處理  
        re = Integer.parseInt(byteStr, 2);  
    }  
    return (byte) re;  
} 
複製程式碼

2、左移和右移

直接舉例說明:

(1)左移:3左移2

  |0000 0000  0000 0000  0000 0000  0000 0011

  00|0000 0000  0000 0000  0000 0000  0000 1100   空位補0

3<<1=6;3<<2=12;3<<3=24;

由此可看出一個規律:

3x2^1=6;3x2^2=12;3x2^3=24;

(2)右移:6右移2位

        0000 0000  0000 0000  0000 0000  0000 0110|

        0000 0000  0000 0000  0000 0000  0000 0001|10  

空位補0(看最高位,這裡最高位為0),負數最高位為1,補1。

總結:<<左移:就是乘以2的移動的位數次冪。

    >>右移:就是除以2的移動的位數次冪。

    >>:最高位補什麼由原有資料的最高位值而定,補0或1。

    >>>無符號右移:無論最高位是什麼,右移後都補0。

3、與(&)、或(|)、異或(^)

直接舉例說明:

(1)6&3=2;

   110             1代表真,0代表假

  & 011   

     010=2

(2)6|5=7;

     110

    |   101   

        111=7

(3) 6^5=3;

      110

     ^   101    

          011=3

    再舉一個例子: 7^4^4=7;  (一個數異或同一個數兩次,結果還是那個數,可以用來資料加密)

       111

     ^   100    

          011

     ^   100    

          111=7