Hutool Hutool
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(hutool-core)

    • AI(Hutool-ai)

    • 注解(annotation)

    • 数组(array)

    • JavaBean(bean)

    • 缓存(cache)

    • 类加载器(classloader)

    • 编码解码(codec)

    • 集合(collection)

    • 比较器(comparator)

    • 压缩(compress)

    • 类型转换(convert)

    • 数据(data)

    • 日期时间(date)

    • 异常(exception)

    • 函数(func)

    • IO流(io)

    • 语言特性(lang)

    • Map(map)

    • 数字数学(math)

      • 数字数学
      • NumberUtil
      • MathUtil
      • BitStatusUtil
        • 主要功能
        • 方法示例
          • 状态定义
          • add(int states, int stat)
          • has(int states, int stat)
          • remove(int states, int stat)
          • clear()
        • 位运算原理
        • 注意事项
        • 应用场景
        • 示例:权限管理
        • 总结
    • 网络(net)

    • 对象池(pool)

    • 反射(reflect)

    • 正则(regex)

    • 服务提供(spi)

    • 聚合操作(stream)

    • 字符串文本(text)

    • 并发和线程(thread)

    • 树结构(tree)

    • 工具集合(util)

    • XML操作(xml)

  • 定时任务(hutool-cron)

  • 加密(hutool-crypto)

  • 数据库(hutool-db)

  • 扩展(hutool-extra)

  • HTTP(hutool-http)

  • 日志(hutool-log)

  • 配置文件(hutool-setting)

  • JSON(hutool-json)

  • Office办公(hutool-poi)

  • 套接字(hutool-socket)

  • GUI(hutool-swing)

  • 指南
  • 核心(hutool-core)
  • 数字数学(math)
Hutool
2026-01-03
目录

BitStatusUtil

BitStatusUtil是Hutool中用于通过位运算表示状态的工具类,提供了添加状态、判断状态、删除状态和清空状态等功能。

# 主要功能

  • 添加状态:通过位运算添加状态
  • 判断状态:判断是否包含指定状态
  • 删除状态:通过位运算删除状态
  • 清空状态:清空所有状态

# 方法示例

# 状态定义

首先,我们需要定义一些状态常量,这些常量必须是偶数且大于等于0:

// 定义状态常量
public static final int STATUS_A = 2;   // 二进制: 10
public static final int STATUS_B = 4;   // 二进制: 100
public static final int STATUS_C = 8;   // 二进制: 1000
public static final int STATUS_D = 16;  // 二进制: 10000

# add(int states, int stat)

增加状态

int status = 0; // 初始状态

// 添加状态A
status = BitStatusUtil.add(status, STATUS_A);
// 状态值: 2 (二进制: 10)

// 添加状态B
status = BitStatusUtil.add(status, STATUS_B);
// 状态值: 6 (二进制: 110)

// 添加状态C
status = BitStatusUtil.add(status, STATUS_C);
// 状态值: 14 (二进制: 1110)

# has(int states, int stat)

判断是否含有状态

int status = 6; // 二进制: 110

// 判断是否包含状态A
boolean hasA = BitStatusUtil.has(status, STATUS_A);
// 输出: true

// 判断是否包含状态B
boolean hasB = BitStatusUtil.has(status, STATUS_B);
// 输出: true

// 判断是否包含状态C
boolean hasC = BitStatusUtil.has(status, STATUS_C);
// 输出: false

# remove(int states, int stat)

删除一个状态

int status = 14; // 二进制: 1110

// 删除状态B
status = BitStatusUtil.remove(status, STATUS_B);
// 状态值: 10 (二进制: 1010)

// 再次删除状态B(已不存在,状态不变)
status = BitStatusUtil.remove(status, STATUS_B);
// 状态值: 10 (二进制: 1010)

# clear()

清空状态

int status = 14; // 二进制: 1110

// 清空状态
status = BitStatusUtil.clear();
// 状态值: 0 (二进制: 0)

# 位运算原理

BitStatusUtil基于位运算实现状态管理,主要使用以下位运算操作:

  1. 添加状态:使用 | (或运算)

    // 添加状态A(10)到状态0(00)
    00 | 10 = 10
    
    // 添加状态B(100)到状态10(10)
    10 | 100 = 110
    
  2. 判断状态:使用 & (与运算)

    // 判断状态110是否包含状态A(10)
    110 & 10 = 10 == 10 → true
    
    // 判断状态110是否包含状态C(1000)
    110 & 1000 = 0 != 1000 → false
    
  3. 删除状态:使用 ^ (异或运算)

    // 从状态110中删除状态B(100)
    110 ^ 100 = 10
    

# 注意事项

  1. 状态值限制:

    • 状态值必须是偶数
    • 状态值必须大于等于0
    • 每个状态值只能占用一个二进制位
  2. 状态值选择:

    • 建议使用2的幂作为状态值,如2、4、8、16、32等
    • 避免使用0作为状态值,因为0在二进制中所有位都是0,无法有效区分"无状态"和"某个特定状态"
  3. 状态数量限制:

    • 使用int类型时,最多支持30个不同的状态(因为int有32位,其中1位用于符号,1位预留)
    • 使用long类型时,最多支持62个不同的状态
  4. 状态冲突:

    • 确保每个状态值只占用一个二进制位,否则会导致状态冲突
    • 例如,不要使用6(二进制: 110)作为状态值,因为它会占用两个二进制位

# 应用场景

BitStatusUtil适用于以下场景:

  1. 权限管理:使用位运算表示用户的不同权限
  2. 状态标记:使用位运算表示对象的不同状态
  3. 开关控制:使用位运算表示多个开关的状态
  4. 特征标记:使用位运算表示对象的不同特征
  5. 配置选项:使用位运算表示多个配置选项的状态

# 示例:权限管理

// 定义权限常量
public static final int PERMISSION_READ = 2;    // 读权限
public static final int PERMISSION_WRITE = 4;   // 写权限
public static final int PERMISSION_EXECUTE = 8; // 执行权限
public static final int PERMISSION_DELETE = 16; // 删除权限

// 用户权限管理
int userPermissions = 0;

// 授予读权限和写权限
userPermissions = BitStatusUtil.add(userPermissions, PERMISSION_READ);
userPermissions = BitStatusUtil.add(userPermissions, PERMISSION_WRITE);

// 检查用户是否有读权限
if (BitStatusUtil.has(userPermissions, PERMISSION_READ)) {
    Console.log("用户有读权限");
}

// 检查用户是否有执行权限
if (BitStatusUtil.has(userPermissions, PERMISSION_EXECUTE)) {
    Console.log("用户有执行权限");
} else {
    Console.log("用户没有执行权限");
}

// 授予执行权限
userPermissions = BitStatusUtil.add(userPermissions, PERMISSION_EXECUTE);

// 撤销写权限
userPermissions = BitStatusUtil.remove(userPermissions, PERMISSION_WRITE);

# 总结

BitStatusUtil是一个轻量级的位状态管理工具类,通过位运算实现高效的状态管理。它具有以下优点:

  1. 高效:位运算的执行速度非常快
  2. 节省内存:使用一个整数即可表示多个状态
  3. 简洁:提供了简洁易用的API
  4. 灵活:可以根据需要动态添加、删除和检查状态

无论是权限管理、状态标记还是开关控制,BitStatusUtil都能提供高效简洁的解决方案。

MathUtil
网络

← MathUtil 网络→

Theme by Vdoing | Copyright © 2025-2026 Hutool | Apache-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式