java 将 byte 或 int 转换为 bitset

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11820402/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:28:55  来源:igfitidea点击:

Convert a byte or int to bitset

javatype-conversionbitarray

提问by moesef

I have the following:

我有以下几点:

int num=Integer.parseInt(lineArray[0]);
byte numBit= num & 0xFF;

Is there any very simple way to convert numBitto a bit array? Or even better, is there a way to bypass the byte conversion of the int and go straigh from numto a bit array?

有什么非常简单的方法可以转换numBit为位数组吗?或者更好的是,有没有办法绕过 int 的字节转换并直接从num位数组转换?

Thanks

谢谢

回答by oldrinb

If you want a BitSet, try:

如果你想要一个BitSet,请尝试:

final byte b = ...;
final BitSet set = BitSet.valueOf(new byte[] { b });

If you want a boolean[],

如果你想要一个boolean[],

static boolean[] bits(byte b) {
  int n = 8;
  final boolean[] set = new boolean[n];
  while (--n >= 0) {
    set[n] = (b & 0x80) != 0;
    b <<= 1;
  }
  return set;
}

or, equivalently,

或者,等效地,

static boolean[] bits(final byte b) {
  return new boolean[] {
    (b &    1) != 0,
    (b &    2) != 0,
    (b &    4) != 0,
    (b &    8) != 0,
    (b & 0x10) != 0,
    (b & 0x20) != 0,
    (b & 0x40) != 0,
    (b & 0x80) != 0
  };
}

回答by Grigory Kislin

Java 7 has BitSet.valueOf(long[]) and BitSet.toLongArray()

Java 7 有 BitSet.valueOf(long[]) 和 BitSet.toLongArray()

int n = 12345;
BitSet bs = BitSet.valueOf(new long[]{n});

回答by Cratylus

You could do:

你可以这样做:

char[] bits = Integer.toBinaryString(num).toCharArray();to get the underlying bit string as a char[]

char[] bits = Integer.toBinaryString(num).toCharArray();将底层位串作为 char[]

E.g.

例如

public BitSet getBitSet(int num){
    char[] bits = Integer.toBinaryString(num).toCharArray();  
    BitSet bitSet = new BitSet(bits.length);  
    for(int i = 0; i < bits.length; i++){  
        if(bits[i] == '1'){
            bitSet.set(i, true);
        }
        else{
            bitSet.set(i, false);
        }                
    }
    return bitSet;
}  

You could create boolean []array also this way.

您也可以通过boolean []这种方式创建数组。

回答by Jaykob

I came about this thread because Android added the BitSet.valueOf()as late as in API 19. I used oldrinb's 2nd snippet of the accepted answer but had to modify it because it had some errors. Additionally I modified it to return a BitSet, but it shouldn't be a problem to change it to boolean[]. See my comment to his reply.

我来到这个线程是因为 AndroidBitSet.valueOf()在 API 19 中添加了最晚的内容。我使用了已接受答案的 oldrinb 的第二个片段,但不得不修改它,因为它有一些错误。此外,我修改了它以返回一个 BitSet,但将其更改为 boolean[] 应该不是问题。请参阅我对他的回复的评论。

This is the modification that now runs successfully:

这是现在运行成功的修改:

public static BitSet toBitSet(byte b) {
    int n = 8;
    final BitSet set = new BitSet(n);
    while (n-- > 0) {
        boolean isSet = (b & 0x80) != 0;
        set.set(n, isSet);
        b <<= 1;
    }
    return set;
}

回答by charlie

Just an excercise in using streams(J8+):

只是使用的练习(J8+):

// J7+
BitSet bitSet(final long... nums) {
    return BitSet.valueOf(nums);
}

// J8+
final IntStream bitsSet = bitSet(num).stream();

// vice-versa
BitSet bitSet(final IntStream bitsSet) {
    return bitsSet.collect(BitSet::new, BitSet::set, BitSet::or);
}

// without BitSet
IntStream bitsSet(final long... nums) {
    return IntStream.range(0, nums.length)
            .flatMap(n -> IntStream.range(0, Long.SIZE - 1)
                    .filter(i -> 0 != (nums[n] & 1L << i))
                    .map(i -> i + n * Long.SIZE));
}