Java BitSet 和整数/长整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2473597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BitSet to and from integer/long
提问by ataylor
If I have an integer that I'd like to perform bit manipulation on, how can I load it into a java.util.BitSet
? How can I convert it back to an int or long? I'm not so concerned about the size of the BitSet
-- it will always be 32 or 64 bits long. I'd just like to use the set()
, clear()
, nextSetBit()
, and nextClearBit()
methods rather than bitwise operators, but I can't find an easy way to initialize a bit set with a numeric type.
如果我有一个想要对其进行位操作的整数,如何将其加载到java.util.BitSet
? 如何将其转换回 int 或 long?我不太关心BitSet
它的大小——它总是 32 位或 64 位长。我只想使用set()
, clear()
, nextSetBit()
, 和nextClearBit()
方法而不是按位运算符,但我找不到一种简单的方法来初始化一个数字类型的位集。
采纳答案by Arne Burmeister
The following code creates a bit set from a long value and vice versa:
以下代码从 long 值创建一个位集,反之亦然:
public class Bits {
public static BitSet convert(long value) {
BitSet bits = new BitSet();
int index = 0;
while (value != 0L) {
if (value % 2L != 0) {
bits.set(index);
}
++index;
value = value >>> 1;
}
return bits;
}
public static long convert(BitSet bits) {
long value = 0L;
for (int i = 0; i < bits.length(); ++i) {
value += bits.get(i) ? (1L << i) : 0L;
}
return value;
}
}
EDITED: Now both directions, @leftbrain: of cause, you are right
编辑:现在两个方向,@leftbrain:当然,你是对的
回答by kukudas
Isn't the public void set(int bit)
method what your looking for?
public void set(int bit)
方法不是你要找的吗?
回答by finnw
Java 7 has BitSet.valueOf(byte[])
and BitSet.toByteArray()
Java 7 具有BitSet.valueOf(byte[])
和BitSet.toByteArray()
If you are stuck with Java 6 or earlier, you can use BigInteger
if it is not likely to be a performance bottleneck - it has getLowestSetBit
, setBit
and clearBit
methods (the last two will create a new BigInteger
instead of modifying in-place.)
如果您坚持使用 Java 6 或更早版本,BigInteger
如果它不太可能成为性能瓶颈,则可以使用- 它具有getLowestSetBit
,setBit
和clearBit
方法(最后两个将创建一个新的BigInteger
而不是就地修改。)
回答by Grigory Kislin
Add to finnw answer: there are also BitSet.valueOf(long[])
and BitSet.toLongArray()
. So:
添加到 finnw 答案:还有BitSet.valueOf(long[])
和BitSet.toLongArray()
。所以:
int n = 12345;
BitSet bs = BitSet.valueOf(new long[]{n});
long l = bs.toLongArray()[0];
回答by user3799584
Pretty much straight from the documentation of nextSetBit
几乎直接来自 nextSetBit 的文档
value=0;
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
value += (1 << i)
}
回答by charlie
To get a long
back from a smallBitSet
in a 'streamy'way:
为了得到一个long
从后面小BitSet
的“流j”方式:
long l = bitSet.stream()
.takeWhile(i -> i < Long.SIZE)
.mapToLong(i -> 1L << i)
.reduce(0, (a, b) -> a | b);
Vice-versa:
反之亦然:
BitSet bitSet = IntStream.range(0, Long.SIZE - 1)
.filter(i -> 0 != (l & 1L << i))
.collect(BitSet::new, BitSet::set, BitSet::or);
N.B.: Using BitSet::valueOf
and BitSet::toLongArray
is of course easier.
注意:使用BitSet::valueOf
和BitSet::toLongArray
当然更容易。