Example usage for java.util BitSet get

List of usage examples for java.util BitSet get

Introduction

In this page you can find the example usage for java.util BitSet get.

Prototype

public boolean get(int bitIndex) 

Source Link

Document

Returns the value of the bit with the specified index.

Usage

From source file:Main.java

/**
 * converts a BitSet into a binary field
 * used in pack routines/*from   w  w w . ja v a  2s .  c  om*/
 * @param b - the BitSet
 * @param bytes - number of bytes to return
 * @return binary representation
 */
public static byte[] bitSet2byte(BitSet b, int bytes) {
    int len = bytes * 8;

    byte[] d = new byte[bytes];
    for (int i = 0; i < len; i++)
        if (b.get(i + 1))
            d[i >> 3] |= (0x80 >> (i % 8));
    //TODO: review why 2nd & 3rd bit map flags are set here??? 
    if (len > 64)
        d[0] |= 0x80;
    if (len > 128)
        d[8] |= 0x80;
    return d;
}

From source file:Main.java

/**
 * converts a BitSet into a binary field
 * used in pack routines//ww w .  ja  v a 2s .c o m
 * @param b - the BitSet
 * @return binary representation
 */
public static byte[] bitSet2byte(BitSet b) {
    int len = (((b.length() + 62) >> 6) << 6);
    byte[] d = new byte[len >> 3];
    for (int i = 0; i < len; i++)
        if (b.get(i + 1))
            d[i >> 3] |= (0x80 >> (i % 8));
    if (len > 64)
        d[0] |= 0x80;
    if (len > 128)
        d[8] |= 0x80;
    return d;
}

From source file:Main.java

/**
 * bit representation of a BitSet//from   w w w  .  j  ava2 s.  co  m
 * suitable for dumps and debugging
 * @param b - the BitSet
 * @return string representing the bits (i.e. 011010010...)
 */
public static String bitSet2String(BitSet b) {
    int len = b.size();
    len = (len > 128) ? 128 : len;
    StringBuilder d = new StringBuilder(len);
    for (int i = 0; i < len; i++)
        d.append(b.get(i) ? '1' : '0');
    return d.toString();
}

From source file:Main.java

public static int bitSet2Int(BitSet bs) {
    int total = 0;
    int b = bs.length() - 1;
    if (b > 0) {
        int value = (int) Math.pow(2, b);
        for (int i = 0; i <= b; i++) {
            if (bs.get(i))
                total += value;/*from ww w  . jav a  2  s . co  m*/
            value = value >> 1;
        }
    }

    return total;
}

From source file:org.efaps.admin.datamodel.attributetype.BitEnumType.java

/**
 * @param _int  the integer value//w w  w  .  ja  v a2 s.  c o m
 * @param _enum enum to be check if it is selected
 * @return true if selected else false
 */
public static boolean isSelected(final Integer _int, final IBitEnum _enum) {
    boolean ret = false;
    final int idx = _enum.getBitIndex();
    final BitSet bitset = BitEnumType.getBitSet(_int);
    if (bitset.length() > idx) {
        ret = bitset.get(idx);
    }
    return ret;
}

From source file:Main.java

public static byte[] toByteArray(BitSet bits) {
    if (bits == null) {
        return null;
    }/*from w  ww .  j  a va2 s . co  m*/
    byte[] bytes = new byte[bits.length() / 8 + 1];
    for (int i = 0; i < bits.length(); i++) {
        if (bits.get(i)) {
            bytes[(bytes.length) - (i / 8) - 1] |= 1 << (i % 8);
        }
    }
    return bytes;
}

From source file:Main.java

static byte[] toByteArray(BitSet bitSet) {
    int bitCount = bitSet.length();
    byte[] result = new byte[((bitCount + 7) / 8)];
    for (int i = 0; i < bitCount; i++) {
        int arrayIndex = i / 8;
        result[arrayIndex] = (byte) (result[arrayIndex] | ((bitSet.get(i) ? 1 : 0) << (i % 8)));
    }//from w ww .  ja v  a  2 s  .c  om
    return result;
}

From source file:examples.mail.IMAPImportMbox.java

/**
 * Is the message wanted?//from ww w.  j  ava2  s  .  c om
 *
 * @param msgNum the message number
 * @param line the From line
 * @param msgNums the list of wanted message numbers
 * @param contains the list of strings to be contained
 * @return true if the message is wanted
 */
private static boolean wanted(int msgNum, String line, BitSet msgNums, List<String> contains) {
    return (msgNums.isEmpty() && contains.isEmpty()) // no selectors
            || msgNums.get(msgNum) // matches message number
            || listContains(contains, line); // contains string
}

From source file:org.apache.openjpa.util.ArrayStateImage.java

/**
 * Return whether the given images are equivalent from an optimistic
 * locking perspective.//from  w w w.j av a 2  s. c  om
 */
public static boolean sameVersion(Object[] state1, Object[] state2) {
    if (state1 == state2)
        return true;

    // if either state is null, then we report that it is the
    // same: this is because a null version will indicate that
    // there are no loaded fields in the version at all, which
    // indicates that there is nothing to compare
    if (state1 == null || state2 == null)
        return true;

    // check only the fields that are in the loaded set for the
    // first version
    BitSet loaded1 = getLoaded(state1);
    BitSet loaded2 = getLoaded(state2);
    for (int i = 0, max = loaded1.length(); i < max; i++) {
        if (loaded1.get(i) && loaded2.get(i) && !ObjectUtils.equals(state1[i], state2[i]))
            return false;
    }
    return true;
}

From source file:org.efaps.admin.datamodel.attributetype.BitEnumType.java

/**
 * @param _bitIndex bitindex the integer value is wanted for
 * @return integer value representing the bitindex
 *///from www .ja  v  a2 s. c om
public static int getInt4Index(final int _bitIndex) {
    final BitSet bitSet = new BitSet(_bitIndex + 1);
    bitSet.set(_bitIndex);
    int ret = 0;
    for (int i = 0; i < bitSet.length(); ++i) {
        ret += bitSet.get(i) ? 1 << i : 0;
    }
    return ret;
}