Example usage for java.util BitSet length

List of usage examples for java.util BitSet length

Introduction

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

Prototype

public int length() 

Source Link

Document

Returns the "logical size" of this BitSet : the index of the highest set bit in the BitSet plus one.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    BitSet bites = new BitSet();
    bites.set(0);/*from   w  w w  . j  ava  2  s.co m*/
    bites.set(1);
    bites.set(2);
    bites.set(3);

    System.out.println(bites.size());
    System.out.println(bites.length());
}

From source file:BitOHoney.java

public static void main(String args[]) {
    String names[] = { "Java", "Source", "and", "Support" };
    BitSet bits = new BitSet();
    for (int i = 0, n = names.length; i < n; i++) {
        if ((names[i].length() % 2) == 0) {
            bits.set(i);//w  w  w . j  ava 2 s . com
        }
    }
    System.out.println(bits);
    System.out.println("Size : " + bits.size());
    System.out.println("Length: " + bits.length());
    for (int i = 0, n = names.length; i < n; i++) {
        if (!bits.get(i)) {
            System.out.println(names[i] + " is odd");
        }
    }
    BitSet bites = new BitSet();
    bites.set(0);
    bites.set(1);
    bites.set(2);
    bites.set(3);
    bites.andNot(bits);
    System.out.println(bites);
}

From source file:BitOHoney1.java

public static void main(String args[]) {
    String names[] = { "Hershey's Kisses", "Nestle's Crunch", "Snickers", "3 Musketeers", "Milky Way", "Twix",
            "Mr. Goodbar", "Crunchie", "Godiva", "Charleston Chew", "Cadbury's", "Lindt", "Aero", "Hebert",
            "Toberlone", "Smarties", "LifeSavers", "Riesen", "Goobers", "Raisenettes", "Nerds", "Tootsie Roll",
            "Sweet Tarts", "Cotton Candy" };
    BitSet bits = new BitSet();
    for (int i = 0, n = names.length; i < n; i++) {
        if ((names[i].length() % 2) == 0) {
            bits.set(i);//from  w  w w  . j  a  v  a  2s .c  o m
        }
    }
    System.out.println(bits);
    System.out.println("Size  : " + bits.size());
    System.out.println("Length: " + bits.length());
    for (int i = 0, n = names.length; i < n; i++) {
        if (!bits.get(i)) {
            System.out.println(names[i] + " is odd");
        }
    }
    BitSet bites = new BitSet();
    bites.set(0);
    bites.set(1);
    bites.set(2);
    bites.set(3);
    bites.andNot(bits);
    System.out.println(bites);
}

From source file:Main.java

public static void main(String[] args) {

    BitSet bitset1 = new BitSet(8);
    BitSet bitset2 = new BitSet(8);

    // assign values to bitset1
    bitset1.set(0);//  www. jav a2  s .co m
    bitset1.set(1);
    bitset1.set(2);

    // assign values to bitset2
    bitset2.set(2);
    bitset2.set(4);

    // print the sets
    System.out.println("Bitset1:" + bitset1);
    System.out.println("Bitset2:" + bitset2);

    // print the length of each bitset
    System.out.println(bitset1.length());
    System.out.println(bitset2.length());
}

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  w w  . j  ava  2s.c  o  m
    return result;
}

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  w w  w .j  av  a  2  s .  co m*/
            value = value >> 1;
        }
    }

    return total;
}

From source file:Main.java

/**
 * converts a BitSet into a binary field
 * used in pack routines/*from w  w  w .  java 2s.  co 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

public static byte[] toByteArray(BitSet bits) {
    if (bits == null) {
        return null;
    }/*ww  w .ja v  a2s  .  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:org.efaps.admin.datamodel.attributetype.BitEnumType.java

/**
 * @param _int  the integer value//from w w w  .j  a v a 2  s  . co 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:org.apache.openjpa.util.ArrayStateImage.java

/**
 * Return whether the given images are equivalent from an optimistic
 * locking perspective.//  w  w w . ja  va2  s  .co  m
 */
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;
}