Example usage for java.util BitSet BitSet

List of usage examples for java.util BitSet BitSet

Introduction

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

Prototype

private BitSet(long[] words) 

Source Link

Document

Creates a bit set using words as the internal representation.

Usage

From source file:TwoBitPlanets.java

public static void main(String args[]) {
    String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };
    int moons[] = { 0, 0, 1, 2, 16, 18, 17, 8, 1 };
    int namesLen = names.length;
    BitSet bits = new BitSet(namesLen);
    for (int i = 0; i < namesLen; i++) {
        if ((moons[i] % 2) == 0) {
            bits.set(i);// w  w  w.  ja  v a2 s.co  m
        }
    }
    for (int i = 0; i < namesLen; i++) {
        System.out.println(names[i] + " Even # Moons (" + moons[i] + ")? " + bits.get(i));
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    BitSet bits1 = new BitSet(16);
    BitSet bits2 = new BitSet(16);

    // set some bits
    for (int i = 0; i < 16; i++) {
        if ((i % 2) == 0)
            bits1.set(i);/* ww w. java  2 s.  c  o m*/
        if ((i % 5) != 0)
            bits2.set(i);
    }

    System.out.println("Initial pattern in bits1: ");
    System.out.println(bits1);
    System.out.println("\nInitial pattern in bits2: ");
    System.out.println(bits2);

    // AND bits
    bits2.and(bits1);
    System.out.println("\nbits2 AND bits1: ");
    System.out.println(bits2);

    // OR bits
    bits2.or(bits1);
    System.out.println("\nbits2 OR bits1: ");
    System.out.println(bits2);

    // XOR bits
    bits2.xor(bits1);
    System.out.println("\nbits2 XOR bits1: ");
    System.out.println(bits2);
}

From source file:Sieve.java

public static void main(String[] s) {
    int n = 2000000;
    long start = System.currentTimeMillis();
    BitSet b = new BitSet(n + 1);
    int count = 0;
    int i;/*from  w  ww  .j  a  va2s  .com*/
    for (i = 2; i <= n; i++)
        b.set(i);
    i = 2;
    while (i * i <= n) {
        if (b.get(i)) {
            count++;
            int k = 2 * i;
            while (k <= n) {
                b.clear(k);
                k += i;
            }
        }
        i++;
    }
    while (i <= n) {
        if (b.get(i))
            count++;
        i++;
    }
    long end = System.currentTimeMillis();
    System.out.println(count + " primes");
    System.out.println((end - start) + " milliseconds");
}

From source file:Main.java

public static BitSet bitSetOf(int... values) {
    BitSet bitSet = new BitSet(values[values.length - 1] + 1);
    for (int value : values) {
        bitSet.set(value);/*  www  .  j  av  a 2s.  com*/
    }
    return bitSet;
}

From source file:Main.java

public static byte[] encodedSeptetsToUnencodedSeptets(byte[] octetBytes, boolean discardLast) {
    byte newBytes[];
    BitSet bitSet;/*w  w  w.  ja v a2  s.  co  m*/
    int i, j, value1, value2;
    bitSet = new BitSet(octetBytes.length * 8);
    value1 = 0;
    for (i = 0; i < octetBytes.length; i++)
        for (j = 0; j < 8; j++) {
            value1 = (i * 8) + j;
            if ((octetBytes[i] & (1 << j)) != 0)
                bitSet.set(value1);
        }
    value1++;
    value2 = value1 / 7 + ((value1 % 7 != 0) ? 1 : 0); // big diff here
    if (value2 == 0)
        value2++;
    newBytes = new byte[value2];
    for (i = 0; i < value2; i++) {
        for (j = 0; j < 7; j++) {
            if ((value1 + 1) > (i * 7 + j)) {
                if (bitSet.get(i * 7 + j)) {
                    newBytes[i] |= (byte) (1 << j);
                }
            }
        }
    }
    if (discardLast && octetBytes.length * 8 % 7 > 0) {
        // when decoding a 7bit encoded string 
        // the last septet may become 0, this should be discarded
        // since this is an artifact of the encoding not part of the 
        // original string
        // this is only done for decoding 7bit encoded text NOT for
        // reversing octets to septets (e.g. for the encoding the UDH)
        if (newBytes[newBytes.length - 1] == 0) {
            byte[] retVal = new byte[newBytes.length - 1];
            System.arraycopy(newBytes, 0, retVal, 0, retVal.length);
            return retVal;
        }
    }
    return newBytes;
}

From source file:Main.java

public static BitSet toBitSet(byte[] array) {
    BitSet bitSet = new BitSet(8 * array.length);

    for (int byteNo = 0; byteNo < array.length; byteNo++) {
        byte b = array[byteNo];

        for (int bitNo = 0; bitNo < 8; bitNo++) {
            if ((b & byteMask(bitNo)) != 0) {
                bitSet.set(8 * byteNo + bitNo);
            }//  w  w  w.  j  av a  2  s.c om
        }
    }

    return bitSet;
}

From source file:HolidaySked.java

public HolidaySked() {
    sked = new BitSet(365);
    int[] holiday = { 1, 15, 50, 148, 185, 246, 281, 316, 326, 359 };
    for (int i = 0; i < holiday.length; i++) {
        addHoliday(holiday[i]);// www . j  a  va 2s . co  m
    }
}

From source file:Main.java

private static BitSet initChars(String specials) {
    BitSet bs = new BitSet(128);
    for (char ch = 33; ch < 127; ch++) {
        if (specials.indexOf(ch) == -1) {
            bs.set(ch);/*  ww w  . j a v a 2s .  c  o  m*/
        }
    }
    return bs;
}

From source file:Main.java

/**
 * Converts a binary representation of a Bitmap field
 * into a Java BitSet//from   w w  w.j  a v a 2s. c o m
 * @param b - binary representation
 * @param offset - staring offset
 * @param bitZeroMeansExtended - true for ISO-8583
 * @return java BitSet object
 */
public static BitSet byte2BitSet(byte[] b, int offset, boolean bitZeroMeansExtended) {
    int len = bitZeroMeansExtended ? ((b[offset] & 0x80) == 0x80 ? 128 : 64) : 64;
    BitSet bmap = new BitSet(len);
    for (int i = 0; i < len; i++)
        if (((b[offset + (i >> 3)]) & (0x80 >> (i % 8))) > 0)
            bmap.set(i + 1);
    return bmap;
}

From source file:Main.java

/**
 * Converts an ASCII representation of a Bitmap field
 * into a Java BitSet/*from   w  w w.j  a v  a 2 s .  c  om*/
 * @param b - hex representation
 * @param offset - starting offset
 * @param bitZeroMeansExtended - true for ISO-8583
 * @return java BitSet object
 */
public static BitSet hex2BitSet(byte[] b, int offset, boolean bitZeroMeansExtended) {
    int len = bitZeroMeansExtended ? ((Character.digit((char) b[offset], 16) & 0x08) == 8 ? 128 : 64) : 64;
    BitSet bmap = new BitSet(len);
    for (int i = 0; i < len; i++) {
        int digit = Character.digit((char) b[offset + (i >> 2)], 16);
        if ((digit & (0x08 >> (i % 4))) > 0)
            bmap.set(i + 1);
    }
    return bmap;
}