Example usage for java.util BitSet set

List of usage examples for java.util BitSet set

Introduction

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

Prototype

public void set(int bitIndex) 

Source Link

Document

Sets the bit at the specified index to true .

Usage

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);
    bitset1.set(1);//from  ww w.ja  va 2  s.c  o m
    bitset1.set(2);

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

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

    // clear from index 2 to index 4 in bitset1
    bitset1.clear(1, 4);

    // clear index 2 to index 8 in bitset2
    bitset2.clear(2, 8);

    // print new bitsets
    System.out.println(bitset1);
    System.out.println(bitset2);

}

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 .ja  v  a 2s  .c  o  m
    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: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);
        }/*from  w ww . j ava2 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);
        }/*w  w w .  jav  a  2  s .co 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: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);
        if ((i % 5) != 0)
            bits2.set(i);/*ww  w  .  j a va  2  s.  c  o  m*/
    }

    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:examples.mail.IMAPImportMbox.java

public static void main(String[] args) throws IOException {
    if (args.length < 2) {
        System.err.println(/*from w  ww . j  a v  a 2s . c o  m*/
                "Usage: IMAPImportMbox imap[s]://user:password@host[:port]/folder/path <mboxfile> [selectors]");
        System.err.println("\tWhere: a selector is a list of numbers/number ranges - 1,2,3-10"
                + " - or a list of strings to match in the initial From line");
        System.exit(1);
    }

    final URI uri = URI.create(args[0]);
    final String file = args[1];

    final File mbox = new File(file);
    if (!mbox.isFile() || !mbox.canRead()) {
        throw new IOException("Cannot read mailbox file: " + mbox);
    }

    String path = uri.getPath();
    if (path == null || path.length() < 1) {
        throw new IllegalArgumentException("Invalid folderPath: '" + path + "'");
    }
    String folder = path.substring(1); // skip the leading /

    List<String> contains = new ArrayList<String>(); // list of strings to find
    BitSet msgNums = new BitSet(); // list of message numbers

    for (int i = 2; i < args.length; i++) {
        String arg = args[i];
        if (arg.matches("\\d+(-\\d+)?(,\\d+(-\\d+)?)*")) { // number,m-n
            for (String entry : arg.split(",")) {
                String[] parts = entry.split("-");
                if (parts.length == 2) { // m-n
                    int low = Integer.parseInt(parts[0]);
                    int high = Integer.parseInt(parts[1]);
                    for (int j = low; j <= high; j++) {
                        msgNums.set(j);
                    }
                } else {
                    msgNums.set(Integer.parseInt(entry));
                }
            }
        } else {
            contains.add(arg); // not a number/number range
        }
    }
    //        System.out.println(msgNums.toString());
    //        System.out.println(java.util.Arrays.toString(contains.toArray()));

    // Connect and login
    final IMAPClient imap = IMAPUtils.imapLogin(uri, 10000, null);

    int total = 0;
    int loaded = 0;
    try {
        imap.setSoTimeout(6000);

        final BufferedReader br = new BufferedReader(new FileReader(file)); // TODO charset?

        String line;
        StringBuilder sb = new StringBuilder();
        boolean wanted = false; // Skip any leading rubbish
        while ((line = br.readLine()) != null) {
            if (line.startsWith("From ")) { // start of message; i.e. end of previous (if any)
                if (process(sb, imap, folder, total)) { // process previous message (if any)
                    loaded++;
                }
                sb.setLength(0);
                total++;
                wanted = wanted(total, line, msgNums, contains);
            } else if (startsWith(line, PATFROM)) { // Unescape ">+From " in body text
                line = line.substring(1);
            }
            // TODO process first Received: line to determine arrival date?
            if (wanted) {
                sb.append(line);
                sb.append(CRLF);
            }
        }
        br.close();
        if (wanted && process(sb, imap, folder, total)) { // last message (if any)
            loaded++;
        }
    } catch (IOException e) {
        System.out.println(imap.getReplyString());
        e.printStackTrace();
        System.exit(10);
        return;
    } finally {
        imap.logout();
        imap.disconnect();
    }
    System.out.println("Processed " + total + " messages, loaded " + loaded);
}

From source file:Main.java

public static BitSet bitSetOfIndexes(int... indexes) {
    BitSet bitSet = new BitSet();
    for (int index : indexes) {
        bitSet.set(index);
    }/*  ww  w  .  j a v  a  2  s  . c o m*/
    return bitSet;
}

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);
    }/*from  w w  w . jav  a  2  s .  c  o m*/
    return bitSet;
}

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);
        }/* w ww.  j a  v  a  2  s . c  o m*/
    }
    return bs;
}

From source file:Main.java

public static BitSet fromByteArray(byte[] bytes) {
    BitSet bits = new BitSet();
    for (int i = 0; i < bytes.length * 8; i++) {
        if ((bytes[bytes.length - i / 8 - 1] & (1 << (i % 8))) > 0) {
            bits.set(i);
        }/*from  w  ww  .ja  v  a 2s . com*/
    }
    return bits;
}