Class BitUtil

java.lang.Object
com.sure.tool.util.BitUtil

public class BitUtil extends Object
位操作工具类:单个位读写与整数位范围提取,零依赖。
Since:
0.2.0
Author:
suretool
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    clear(int value, int pos)
    清除 int 指定位(置 0)。
    static int
    get(byte[] data, int pos)
    获取字节数组指定位(位序按大端:index 0 为第 1 字节最高位)。
    static int
    get(int value, int pos)
    获取 int 指定位(0 为最低位)。
    static int
    get(long value, int pos)
    获取 long 指定位(0 为最低位)。
    static int
    getRange(int value, int from, int to)
    提取 int 的位范围 [from, to)(from 为低位),右对齐返回。
    static long
    getRange(long value, int from, int to)
    提取 long 的位范围 [from, to)。
    static int
    set(int value, int pos)
    设置 int 指定位为 1。
    static long
    set(long value, int pos)
    设置 long 指定位为 1。
    static int
    toggle(int value, int pos)
    翻转 int 指定位。

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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:
      提取的位(右对齐)