Java 中可以声明 1 位变量吗?

发布于 2024-12-26 07:22:30 字数 148 浏览 1 评论 0原文

我的算法使用一个巨大的布尔数组,正如我所学的那样,每个布尔变量占用 1 个字节。无论如何,有没有声明一个布尔数组并减少内存使用,因为我正在电话环境中工作。

编辑:我和我的朋友正在讨论 BitSet 是否比普通布尔数组慢。请澄清这一点。该算法仍然需要性能作为最佳需求。

My algorithm use a huge array of boolean, and as I was taught, it take 1 byte for each boolean variable. Is there anyway to declare a boolean array and reduce the memory usage, because I'm working on phone environment.

EDIT: My friend and I are discussing if BitSet is slower than normal Boolean array. Please clarify this. The algorithm still needs performance as best demand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气称霸 2025-01-02 07:22:30

BitSet

此类实现了一个根据需要增长的位向量。每个
位组的组成部分具有布尔值。 BitSet 的位是
由非负整数索引。单独的索引位可以是
检查、设置或清除。一个 BitSet 可用于修改
另一个 BitSet 的内容通过逻辑 AND、逻辑 OR、
和逻辑异或运算。

使用 boolean之间的基准测试链接>位集

BitSet

This class implements a vector of bits that grows as needed. Each
component of the bit set has a boolean value. The bits of a BitSet are
indexed by nonnegative integers. Individual indexed bits can be
examined, set, or cleared. One BitSet may be used to modify the
contents of another BitSet through logical AND, logical inclusive OR,
and logical exclusive OR operations.

Link to benchmark between using boolean versus BitSet

dawn曙光 2025-01-02 07:22:30

您也可以使用 EnumSet 。这允许您使用命名位,并且比使用使用索引位的 BitSet 更友好。

与枚举类型一起使用的专用 Set 实现。枚举集中的所有元素必须来自创建该集合时显式或隐式指定的单个枚举类型。枚举集在内部表示为位向量。这种表示方式极其紧凑且高效。该类的空间和时间性能应该足够好,以允许其用作传统的基于 int 的“位标志”的高质量、类型安全的替代品。即使是批量操作(例如 containsAll 和 keepAll),如果它们的参数也是枚举集,也应该运行得非常快。

例如,

BitSet bs = new BitSet(4);
bs.set(1); // READY
bs.set(3); // LARGE_FLAG
boolean largeFlag = bs.get(1); // LARGE_FLAG
System.out.println("Using BitSet: "+bs);

EnumSet<Settings> settings = EnumSet.noneOf(Settings.class);
settings.add(Settings.READY);
settings.add(Settings.LARGE_FLAG);
boolean largeFlag2 = settings.contains(Settings.LARGE_FLAG);
System.out.println("Using EnumSet: "+settings);

如果合适的话,打印

Using BitSet: {1, 3}
Using EnumSet: [READY, LARGE_FLAG]

IMHO EnumSet 会更清晰。

You can use a EnumSet as well. This allows you to use named bits and can be friendlier than using BitSet which uses indexed bits.

A specialized Set implementation for use with enum types. All of the elements in an enum set must come from a single enum type that is specified, explicitly or implicitly, when the set is created. Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based "bit flags." Even bulk operations (such as containsAll and retainAll) should run very quickly if their argument is also an enum set.

e.g.

BitSet bs = new BitSet(4);
bs.set(1); // READY
bs.set(3); // LARGE_FLAG
boolean largeFlag = bs.get(1); // LARGE_FLAG
System.out.println("Using BitSet: "+bs);

EnumSet<Settings> settings = EnumSet.noneOf(Settings.class);
settings.add(Settings.READY);
settings.add(Settings.LARGE_FLAG);
boolean largeFlag2 = settings.contains(Settings.LARGE_FLAG);
System.out.println("Using EnumSet: "+settings);

prints

Using BitSet: {1, 3}
Using EnumSet: [READY, LARGE_FLAG]

IMHO EnumSet is much clearer if its appropriate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文