java常用位操作

2021-08-13 08:27:05 字數 846 閱讀 4494

public

class

testindex else

if (len == 0)

return result;

}/**

* 將乙個高位在前的位元組陣列轉為int

*@param num

*@return

*/public

static

byte inttobytes(int num)

/*** 將乙個高位在前的位元組陣列轉為int

*@param bytes

*@return

*/public

static

intbytetoint(byte bytes)

/*** 顯示乙個byte陣列

*@param bytes

*/public

static

void

showbyte(byte bytes)

system.out.println();

}/**

* 將byte轉換為乙個長度為8的byte陣列,陣列每個值代表bit

*/public

static

byte getbooleanarray(byte b)

return array;

} /**

* 把byte轉為字串的bit

*/public

static string bytetobit(byte b)

public

static

void

main(string args)

}

常用位操作

參加運算的兩個資料,按二進位制位進行 與 運算。運算規則 0 0 0 0 1 0 1 0 0 1 1 1 即 兩位同時為 1 結果才為 1 否則為0 例如 3 5 即 0000 0011 0000 0101 0000 0001 因此,3 5的值得1。另,負數按補碼形式參加按位與運算。與運算 的特殊用...

常用位操作

x 0s x x 0s 0 x 0s x x 1s x x 1s x x 1s 1s x x 0 x x x x x x a b a b a a b b 該方法將1左移i位,得到形如00010000的值。接著,對這個值和num執行 位與 操作,從而將i位之外的所有位清零。最後,檢查該結果是否為零。不...

常用的位操作

191.位1的個數 231.2的冪 本文分兩部分,第一部分列舉幾個有趣的位操作,第二部分講解演算法中常用的 n n 1 操作,順便把用到這個技巧的演算法題列出來講解一下。因為位操作很簡單,所以假設讀者已經了解與 或 異或這三種基本操作。位操作 bit manipulation 可以玩出很多奇技淫巧,...