e129. performing bitwise operations with biginteger.txt
来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 40 行
TXT
40 行
A BigInteger object is immutable. For a mutable object that supports bitwise operations, see e363 Performing Bitwise Operations on a Bit Vector.
// Create via an array of bytes in twos-complement form.
// The byte-ordering is big-endian which means the most significant bit is in element 0.
// A negative value
byte[] bytes = new byte[]{(byte)0xFF, 0x00, 0x00}; // -65536
// A positive value
bytes = new byte[]{0x1, 0x00, 0x00}; // 65536
BigInteger bi = new BigInteger(bytes);
// Get the value of a bit
boolean b = bi.testBit(3); // 0
b = bi.testBit(16); // 1
// Set a bit
bi = bi.setBit(3);
// Clear a bit
bi = bi.clearBit(3);
// Flip a bit
bi = bi.flipBit(3);
// Shift the bits
bi = bi.shiftLeft(3);
bi = bi.shiftRight(1);
// Other bitwise operations
bi = bi.xor(bi);
bi = bi.and(bi);
bi = bi.not();
bi = bi.or(bi);
bi = bi.andNot(bi);
// Retrieve the current bits in a byte array in twos-complement form.
// The byte-ordering is big-endian which means the most significant bit is in element 0.
bytes = bi.toByteArray();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?