Class BitUtil
java.lang.Object
com.sure.tool.util.BitUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic intclear(int value, int pos) 清除 int 指定位(置 0)。static intget(byte[] data, int pos) 获取字节数组指定位(位序按大端:index 0 为第 1 字节最高位)。static intget(int value, int pos) 获取 int 指定位(0 为最低位)。static intget(long value, int pos) 获取 long 指定位(0 为最低位)。static intgetRange(int value, int from, int to) 提取 int 的位范围 [from, to)(from 为低位),右对齐返回。static longgetRange(long value, int from, int to) 提取 long 的位范围 [from, to)。static intset(int value, int pos) 设置 int 指定位为 1。static longset(long value, int pos) 设置 long 指定位为 1。static inttoggle(int value, int pos) 翻转 int 指定位。
-
Method Details
-
get
public static int get(int value, int pos) 获取 int 指定位(0 为最低位)。- Parameters:
value- 值pos- 位位置(0~31)- Returns:
- 该位 0/1
-
get
public static int get(long value, int pos) 获取 long 指定位(0 为最低位)。- Parameters:
value- 值pos- 位位置(0~63)- Returns:
- 该位 0/1
-
get
public static int get(byte[] data, int pos) 获取字节数组指定位(位序按大端:index 0 为第 1 字节最高位)。- Parameters:
data- 字节数组pos- 位位置(0 ~ data.length*8-1)- Returns:
- 该位 0/1
-
set
public static int set(int value, int pos) 设置 int 指定位为 1。- Parameters:
value- 值pos- 位位置(0~31)- Returns:
- 新值
-
clear
public static int clear(int value, int pos) 清除 int 指定位(置 0)。- Parameters:
value- 值pos- 位位置(0~31)- Returns:
- 新值
-
toggle
public static int toggle(int value, int pos) 翻转 int 指定位。- Parameters:
value- 值pos- 位位置(0~31)- Returns:
- 新值
-
set
public static long set(long value, int pos) 设置 long 指定位为 1。- Parameters:
value- 值pos- 位位置(0~63)- Returns:
- 新值
-
getRange
public static int getRange(int value, int from, int to) 提取 int 的位范围 [from, to)(from 为低位),右对齐返回。- Parameters:
value- 值from- 起始位(含,0 为最低位)to- 结束位(不含)- Returns:
- 提取的位(右对齐)
-
getRange
public static long getRange(long value, int from, int to) 提取 long 的位范围 [from, to)。- Parameters:
value- 值from- 起始位(含)to- 结束位(不含)- Returns:
- 提取的位(右对齐)
-