简体   繁体   English

Java位比较,位集?

[英]Java bit comparison, bitset?

I've got the word 'bitset' stuck in my head as the solution to my problem but I think I might be getting myself confused. 解决问题的方法是将“ bitset”这个词插入我的脑海,但我想我可能会感到困惑。

I've got a list of hex values that indicate certain conditions, such as: 我有一个表示特定条件的十六进制值列表,例如:

0x0001 = Outside
0x20000000 = Blah...

Now I'm reading in an int, and I basically want to compare the int against all of the existing hex conditions to see which it matches. 现在,我正在读取一个int,我基本上想将int与所有现有的十六进制条件进行比较,以查看它是否匹配。 It can match zero, one, or many. 它可以匹配零个,一个或多个。

Is a bitset actually what I want, or is there a simpler way of doing it? 我实际上是想要一个位集,还是有一个更简单的方法呢?

I feel a bit silly for asking this, but I can't remember what the sodding thing is called! 问这个问题我感到很傻,但是我不记得所谓的闷闷不乐! :) :)

Many thanks 非常感谢

It's not exactly clear what you want, but the Java SDK does provide a BitSet , along with a number of useful methods for working with BitSets. 尚不清楚您想要什么,但是Java SDK确实提供了BitSet ,以及许多用于使用BitSet的有用方法。 In your case, the and() and intersects() methods might be of use. 在您的情况下,and()和intersects()方法可能有用。

我认为您所追求的词是“ bitmask”

Are you looking for bitmasking? 您在寻找位屏蔽吗? This is where each bit in an int represents a boolean value, set (1, means true) and unset (0, means false). 这是int中的每个位表示一个布尔值的地方,置1(表示真)和未设置(0表示假)。 For example: 例如:

public class MaskingExample {

    private static final int OUTSIDE_MASK = 1; // Right-most bit
    private static final int HEATED_MASK = 1 << 1; // Second-to-right-most bit
    private static final int WET_MASK = 1 << 2; // Third-to-right-most bit

    private int value = 0;

    public boolean isOutside() {
        return isBitSet(OUTSIDE_MASK, value);
    }

    public void setOutside(boolean outside) {
        value = outside ? setBit(OUTSIDE_MASK, value) : unsetBit(OUTSIDE_MASK, value);
    }

    // Other setters and getters

    private static int setBit(int mask, int value) {
        return value | mask;
    }

    private static int unsetBit(int mask, int value) {
        return value & ~mask;
    }

    private static boolean isBitSet(int mask, int value) {
        return (value & mask) == mask;
    }
}

If you need more than 32 conditions, use long for all the masks and value , and add an L to each of the 1 values being shifted, and you can have up to 64 conditions, like this: 如果您需要32个以上的条件,请对所有的mask和value使用long ,并向要移动的1值中的每一个添加L ,这样最多可以有64个条件,如下所示:

private static final long OUTSIDE_MASK = 1L; // Right-most bit
private static final long HEATED_MASK = 1L << 1; // Second-to-right-most bit
private static final long WET_MASK = 1L << 2; // Third-to-right-most bit

private long value = 0;

You can also set more than one bit at a time too, by the way. 顺便说一下,您也可以一次设置多个位。 You combine masks together into a single mask using & : 您可以使用&将蒙版组合成一个蒙版:

public void setOutsideAndRaining(boolean outsideAndRaining) {
    int comboMask = OUTSIDE_MASK & WET_MASK;
    value = outsideAndRaining ? setBit(comboMask, value) : unsetBit(comboMask, value);
}

Edit: After seeing kaliatech's answer below, you could also use BitSet . 编辑:在下面看到kaliatech的答案后,您还可以使用BitSet The solution is very similar, but the math logic is encapsulated in the BitSet object and it allows for an arbitrary number of bits so you aren't limited to only 64. 解决方案非常相似,但是数学逻辑封装在BitSet对象中,并且它允许任意数量的位,因此您不仅限于64位。

public class MaskingExample {

    private static final int OUTSIDE_POSITION = 0;
    private static final int HEATED_POSITION = 1;
    private static final int WET_POSITION = 2;
    private static final int TOTAL_CONDITIONS = 3;

    private BitSet bitSet = new BitSet(TOTAL_CONDITIONS);

    public boolean isOutside() {
        return bitSet.get(OUTSIDE_POSITION);
    }

    public void setOutside(boolean outside) {
        bitSet.set(OUTSIDE_POSITION, outside);
    }

    // Other setters and getters
}

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

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