0

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.

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. 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

Tony
  • 3,587
  • 8
  • 44
  • 77
  • Looks like you're looking for an `enum` and a `switch`? – Mysticial Feb 11 '13 at 17:53
  • 1
    Does a specific bit mean something? If not, I would go with plain `enum`. – ogzd Feb 11 '13 at 17:54
  • Well the problem I've got is there about 40 or so conditions within the 32 bits of the int and I didn't really want to have 40 if statements for each. I've seen something in the past using C code, but I cannot for the life of me remember what it was. – Tony Feb 11 '13 at 17:55
  • so 1 bit can actually represent more than one condition? ( 32 bits, 40 conditions?) or am I missing something here? – axiom Feb 11 '13 at 17:56
  • Sorry, I got my wires crossed, there are 15 conditions, I'll update – Tony Feb 11 '13 at 17:57
  • You can use a `long`, which gives you enough bits for 40 conditions. An `enum` and an `EnumSet` is probably more maintenance-friendly. – Ted Hopp Feb 11 '13 at 17:58
  • Might help ( not sure so not posting as answer) : loop through, shifting the bits right. At each iteration, check if the LSB is set or not, and so you can set condition[i] depending on that. – axiom Feb 11 '13 at 18:00

3 Answers3

1

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. In your case, the and() and intersects() methods might be of use.

kaliatech
  • 17,579
  • 5
  • 72
  • 84
1

I think the word you're after is "bitmask"

Magnus
  • 10,736
  • 5
  • 44
  • 57
1

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). 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:

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. 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.

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
}
Brian
  • 17,079
  • 6
  • 43
  • 66