字節的位操作

1、取出一個字節(byte)中的每一位(bit)

轉載自:https://blog.csdn.net/qq_21794823/article/details/53517628?locationNum=6&fps=1

例:
 
byte byData = 0x36;
 
int n0, n1, n2, n3, n4, n5, n6, n7;
n0 = (byData & 0x01) == 0x01 ? 1 : 0;
n1 = (byData & 0x02) == 0x02 ? 1 : 0;
n2 = (byData & 0x04) == 0x04 ? 1 : 0;
n3 = (byData & 0x08) == 0x08 ? 1 : 0;
n4 = (byData & 0x10) == 0x10 ? 1 : 0;
n5 = (byData & 0x20) == 0x20 ? 1 : 0;
n6 = (byData & 0x40) == 0x40 ? 1 : 0;
n7 = (byData & 0x80) == 0x80 ? 1 : 0;

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

轉載自:https://blog.csdn.net/dlf1769/article/details/78918152

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章