Class OpenBit
java.lang.Object
cloud.opencode.base.core.OpenBit
Bit Manipulation Utility Class - Set, clear, flip, test, count, rotate and mask operations
位操作工具类 - 位设置、清除、翻转、测试、计数、旋转和掩码操作
Provides comprehensive bit manipulation functions for int and long types.
提供全面的位操作功能,支持 int 和 long 类型。
Features | 主要功能:
- Bit setting (setBit, clearBit, flipBit) - 位设置
- Bit testing (testBit) - 位测试
- Bit counting (countBits, countLeadingZeros, countTrailingZeros) - 位计数
- Bit rotation (rotateLeft, rotateRight) - 位旋转
- Bit reversal (reverse, reverseBytes) - 位反转
- Bit field operations (extractField, insertField) - 位字段操作
- Mask creation (createMask, createMaskLong) - 掩码创建
- Power of two checks (isPowerOfTwo, nextPowerOfTwo) - 2 的幂检查
Usage Examples | 使用示例:
// Set/Test bit - 设置/测试位
int value = OpenBit.setBit(0, 3); // 8 (binary: 1000)
boolean set = OpenBit.testBit(8, 3); // true
// Count bits - 计算位数
int ones = OpenBit.countBits(0b1010); // 2
// Power of two - 2 的幂
boolean isPow2 = OpenBit.isPowerOfTwo(16); // true
int next = OpenBit.nextPowerOfTwo(7); // 8
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
- Null-safe: N/A (primitive types) - 空值安全: 不适用 (原始类型)
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic intclearBit(int value, int position) Clears the bit at the specified position 清除指定位置的位static longclearBit(long value, int position) Clears the bit at the specified position (long) 清除指定位置的位(long)static intcountBits(int value) Counts the number of set bits (number of 1s) 计算设置的位数(1 的个数)static intcountBits(long value) Counts the number of set bits (long) 计算设置的位数(long)static intcountLeadingZeros(int value) Counts the number of leading zeros 计算前导零数量static intcountLeadingZeros(long value) Counts the number of leading zeros (long) 计算前导零数量(long)static intcountTrailingZeros(int value) Counts the number of trailing zeros 计算尾随零数量static intcountTrailingZeros(long value) Counts the number of trailing zeros (long) 计算尾随零数量(long)static intcreateMask(int bits) Creates a bit mask 创建位掩码static longcreateMaskLong(int bits) Creates a bit mask (long) 创建位掩码(long)static intextractField(int value, int position, int length) Extracts a bit field 提取位字段static longextractField(long value, int position, int length) Extracts a bit field (long) 提取位字段(long)static intflipBit(int value, int position) Flips the bit at the specified position 翻转指定位置的位static longflipBit(long value, int position) Flips the bit at the specified position (long) 翻转指定位置的位(long)static inthighestOneBitPosition(int value) Gets the position of the highest set bit 获取最高有效位的位置static inthighestOneBitPosition(long value) Gets the position of the highest set bit (long) 获取最高有效位的位置(long)static intinsertField(int value, int fieldValue, int position, int length) Inserts a bit field 插入位字段static longinsertField(long value, long fieldValue, int position, int length) Inserts a bit field (long) 插入位字段(long)static booleanisPowerOfTwo(int value) Checks whether the value is a power of two 检查是否为 2 的幂static booleanisPowerOfTwo(long value) Checks whether the value is a power of two (long) 检查是否为 2 的幂(long)static intlowestOneBitPosition(int value) Gets the position of the lowest set bit 获取最低有效位的位置static intlowestOneBitPosition(long value) Gets the position of the lowest set bit (long) 获取最低有效位的位置(long)static intnextPowerOfTwo(int value) Rounds up to the next power of two 向上舍入到下一个 2 的幂static longnextPowerOfTwo(long value) Rounds up to the next power of two (long) 向上舍入到下一个 2 的幂(long)static intreverse(int value) Reverses all bits 反转所有位static longreverse(long value) Reverses all bits (long) 反转所有位(long)static intreverseBytes(int value) Reverses the byte order 反转字节顺序static longreverseBytes(long value) Reverses the byte order (long) 反转字节顺序(long)static introtateLeft(int value, int distance) Rotates bits to the left 左旋转位static longrotateLeft(long value, int distance) Rotates bits to the left (long) 左旋转位(long)static introtateRight(int value, int distance) Rotates bits to the right 右旋转位static longrotateRight(long value, int distance) Rotates bits to the right (long) 右旋转位(long)static intsetBit(int value, int position) Sets the bit at the specified position 设置指定位置的位static longsetBit(long value, int position) Sets the bit at the specified position (long) 设置指定位置的位(long)static booleantestBit(int value, int position) Tests whether the bit at the specified position is set 测试指定位置的位是否被设置static booleantestBit(long value, int position) Tests whether the bit at the specified position is set (long) 测试指定位置的位是否被设置(long)
-
Method Details
-
setBit
public static int setBit(int value, int position) Sets the bit at the specified position 设置指定位置的位- Parameters:
value- the original value | 原始值position- the bit position (0-31) | 位置(0-31)- Returns:
- the value with the bit set | 设置后的值
-
setBit
public static long setBit(long value, int position) Sets the bit at the specified position (long) 设置指定位置的位(long) -
clearBit
public static int clearBit(int value, int position) Clears the bit at the specified position 清除指定位置的位 -
clearBit
public static long clearBit(long value, int position) Clears the bit at the specified position (long) 清除指定位置的位(long) -
flipBit
public static int flipBit(int value, int position) Flips the bit at the specified position 翻转指定位置的位 -
flipBit
public static long flipBit(long value, int position) Flips the bit at the specified position (long) 翻转指定位置的位(long) -
testBit
public static boolean testBit(int value, int position) Tests whether the bit at the specified position is set 测试指定位置的位是否被设置 -
testBit
public static boolean testBit(long value, int position) Tests whether the bit at the specified position is set (long) 测试指定位置的位是否被设置(long) -
countBits
public static int countBits(int value) Counts the number of set bits (number of 1s) 计算设置的位数(1 的个数) -
countBits
public static int countBits(long value) Counts the number of set bits (long) 计算设置的位数(long) -
countLeadingZeros
public static int countLeadingZeros(int value) Counts the number of leading zeros 计算前导零数量 -
countLeadingZeros
public static int countLeadingZeros(long value) Counts the number of leading zeros (long) 计算前导零数量(long) -
countTrailingZeros
public static int countTrailingZeros(int value) Counts the number of trailing zeros 计算尾随零数量 -
countTrailingZeros
public static int countTrailingZeros(long value) Counts the number of trailing zeros (long) 计算尾随零数量(long) -
rotateLeft
public static int rotateLeft(int value, int distance) Rotates bits to the left 左旋转位 -
rotateLeft
public static long rotateLeft(long value, int distance) Rotates bits to the left (long) 左旋转位(long) -
rotateRight
public static int rotateRight(int value, int distance) Rotates bits to the right 右旋转位 -
rotateRight
public static long rotateRight(long value, int distance) Rotates bits to the right (long) 右旋转位(long) -
reverse
public static int reverse(int value) Reverses all bits 反转所有位 -
reverse
public static long reverse(long value) Reverses all bits (long) 反转所有位(long) -
reverseBytes
public static int reverseBytes(int value) Reverses the byte order 反转字节顺序 -
reverseBytes
public static long reverseBytes(long value) Reverses the byte order (long) 反转字节顺序(long) -
extractField
public static int extractField(int value, int position, int length) Extracts a bit field 提取位字段- Parameters:
value- the original value | 原始值position- the start position | 起始位置length- the length | 长度- Returns:
- the extracted value | 提取的值
-
extractField
public static long extractField(long value, int position, int length) Extracts a bit field (long) 提取位字段(long) -
insertField
public static int insertField(int value, int fieldValue, int position, int length) Inserts a bit field 插入位字段- Parameters:
value- the original value | 原始值fieldValue- the field value to insert | 要插入的字段值position- the start position | 起始位置length- the length | 长度- Returns:
- the value after insertion | 插入后的值
-
insertField
public static long insertField(long value, long fieldValue, int position, int length) Inserts a bit field (long) 插入位字段(long) -
createMask
public static int createMask(int bits) Creates a bit mask 创建位掩码- Parameters:
bits- the number of bits | 位数- Returns:
- the mask | 掩码
-
createMaskLong
public static long createMaskLong(int bits) Creates a bit mask (long) 创建位掩码(long) -
isPowerOfTwo
public static boolean isPowerOfTwo(int value) Checks whether the value is a power of two 检查是否为 2 的幂 -
isPowerOfTwo
public static boolean isPowerOfTwo(long value) Checks whether the value is a power of two (long) 检查是否为 2 的幂(long) -
nextPowerOfTwo
public static int nextPowerOfTwo(int value) Rounds up to the next power of two 向上舍入到下一个 2 的幂 -
nextPowerOfTwo
public static long nextPowerOfTwo(long value) Rounds up to the next power of two (long) 向上舍入到下一个 2 的幂(long) -
highestOneBitPosition
public static int highestOneBitPosition(int value) Gets the position of the highest set bit 获取最高有效位的位置 -
highestOneBitPosition
public static int highestOneBitPosition(long value) Gets the position of the highest set bit (long) 获取最高有效位的位置(long) -
lowestOneBitPosition
public static int lowestOneBitPosition(int value) Gets the position of the lowest set bit 获取最低有效位的位置 -
lowestOneBitPosition
public static int lowestOneBitPosition(long value) Gets the position of the lowest set bit (long) 获取最低有效位的位置(long)
-