简体   繁体   English

在Java中将位向量(布尔数组)转换为整数,并将整数转换为位向量

[英]Convert bit vector (array of booleans) to an integer, and integer to bit vector, in Java

What's the best way to unstub the following functions? 取消以下功能的最佳方法是什么?

// Convert a bit-vector to an integer. 
int bitvec2int(boolean[] b)
{
    [CODE HERE]
}

// Convert an integer x to an n-element bit-vector. 
boolean[] int2bitvec(int x, int n)
{
    [CODE HERE]
}

Or is there a better way to do that sort of thing than passing boolean arrays around? 还是有比传递布尔数组更好的方法呢?

This comes up in an Android app where we need an array of 20 booleans to persist and the easiest way to do that is to write an integer or string to the key-value store. 这是在Android应用程序中出现的,我们需要一个20个布尔值的数组来持久化,而最简单的方法是将一个整数或字符串写入键值存储。

I'll post the way we ( Bee and I) wrote the above as an answer. 我将以我们( Bee和我)写上面的方式作为答案。 Thanks! 谢谢!

Use java.util.BitSet instead. 请改用java.util.BitSet It'd be much faster than dealing with boolean[] . 这比处理boolean[]要快得多。

Also, you should really ask yourself if these 20 boolean really should be enum , in which case you can use EnumSet , which is the Java solution to the bit fields technique from C (see: Effective Java 2nd Edition: Use EnumSet instead of bit fields ). 另外,您应该真正问问自己,这20个boolean真的应该是enum ,在这种情况下,您可以使用EnumSet ,这是C语言中位域技术的Java解决方案(请参阅: 有效的Java 2nd Edition:使用EnumSet代替位域 )。


BitSet to/from int conversion BitSet到/从int转换

You might as well just use BitSet and drop the int , but just in case you need these: 您最好只使用BitSet并删除int ,但以防万一,您需要使用以下这些:

static BitSet toBitSet(int i) {
    BitSet bs = new BitSet(Integer.SIZE);
    for (int k = 0; k < Integer.SIZE; k++) {
        if ((i & (1 << k)) != 0) {
            bs.set(k);
        }
    }
    return bs;
}
static int toInt(BitSet bs) {
    int i = 0;
    for (int pos = -1; (pos = bs.nextSetBit(pos+1)) != -1; ) {
        i |= (1 << pos);
    }
    return i;
}

Two different techniques were deliberately used for instructional purposes. 故意将两种不同的技术用于教学目的。 For robustness, the BitSet to int conversion should ensure that 32 bits is enough. 为了鲁棒性,从BitSetint转换应确保32位足够。


EnumSet example EnumSet示例

This example is based on the one given in the book: 本示例基于书中给出的示例:

import java.util.*;
public enum Style {
    BOLD, ITALIC, UNDERLINE, STRIKETHROUGH;

    public static void main(String[] args) {
        Set<Style> s1 = EnumSet.of(BOLD, UNDERLINE);
        System.out.println(s1); // prints "[BOLD, UNDERLINE]"

        s1.addAll(EnumSet.of(ITALIC, UNDERLINE));
        System.out.println(s1.contains(ITALIC)); // prints "true"
    }
}

From the API : API中

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." 此类的时空性能应足够好,以使其可以用作传统的基于int的“位标志”的高质量,类型安全的替代方法。

// Convert a big-endian bit-vector to an integer. 
int bitvec2int(boolean[] b)
{
    int x = 0;
    for(boolean i : b) x = x << 1 | (i?1:0);
    return x;
}

// Convert an integer x to an n-element big-endian bit-vector. 
boolean[] int2bitvec(int x, int n)
{
    boolean[] b = new boolean[n];
    for(int i = 0; i < n; i++) b[i] = (1 << n-i-1 & x) != 0;
    return b;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM