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:MainClass.java

public static void main(String[] argv) {
    BitSet bs = new BitSet();
    bs.set(65);/*from  w  w w.  jav  a2 s.  co m*/
    System.out.println("Bit 65 is " + bs.get(65));
}

From source file:NumSeries.java

public static void main(String[] args) {
    for (int i = 1; i <= months.length; i++)
        System.out.println("Month # " + i);

    for (int i = 0; i < months.length; i++)
        System.out.println("Month " + months[i]);

    BitSet b = new BitSet();
    b.set(0); // January
    b.set(3); // April

    for (int i = 0; i < months.length; i++) {
        if (b.get(i))
            System.out.println("Month " + months[i] + " requested");
    }//from w ww.j  ava 2  s.  co m
}

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;//www . ja  va2s  .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: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);/*from w ww  . j  ava2 s.  c  o  m*/
        }
    }
    for (int i = 0; i < namesLen; i++) {
        System.out.println(names[i] + " Even # Moons (" + moons[i] + ")? " + bits.get(i));
    }
}

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  w  w  .ja v a2 s  .c  om*/
        }
    }
    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  ww.j  a v  a2  s.  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);//from  www .  j a v  a2s . c om
    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);

    System.out.println(bitset1.get(1));

    System.out.println(bitset2.get(2));

}

From source file:org.jax.bioinfdata.phylogeny.PhylogenySdpMain.java

/**
 * Main entry point for reading in newick trees at specified intervals and
 * turning them into SDPs/*from w ww. j  a v a  2 s . c  o m*/
 * @param args
 * @throws IOException 
 * @throws IllegalFormatException 
 */
public static void main(String[] args) throws IOException, IllegalFormatException {
    // Deal with the options.
    CommandLineParser parser = new GnuParser();
    Options options = new Options();
    CommandLine commandLine = null;

    final Option helpOption;
    {
        helpOption = new Option("help", "Print this help and exit");
        helpOption.setRequired(false);
        options.addOption(helpOption);
    }

    final Option inputFileOption;
    {
        inputFileOption = new Option("phylocsvin",
                "This is the input " + "CSV file which must have a header row allong with "
                        + "the following four columns in order: chromosome ID, "
                        + "interval start in base pairs, interval end in base pairs, "
                        + "newick formatted phylogeny tree");
        inputFileOption.setRequired(true);
        inputFileOption.setArgs(1);
        inputFileOption.setArgName("file name");
        options.addOption(inputFileOption);
    }

    final Option minMinorStrainCountOption;
    {
        minMinorStrainCountOption = new Option("minorallelecountthreshold",
                "this option specifies the minimum minor allele count that "
                        + "an SDP must have. All SDPs that fall below this threshold "
                        + "will be filtered from the output.");
        minMinorStrainCountOption.setRequired(true);
        minMinorStrainCountOption.setArgs(1);
        minMinorStrainCountOption.setArgName("min SDP count");
        options.addOption(minMinorStrainCountOption);
    }

    final Option outputFileOption;
    {
        outputFileOption = new Option("sdpcsvout", "the output CSV file");
        outputFileOption.setRequired(true);
        outputFileOption.setArgs(1);
        outputFileOption.setArgName("file name");
        options.addOption(outputFileOption);
    }

    try {
        commandLine = parser.parse(options, args);

        // See if we just need to print the help options.
        if (commandLine.hasOption(helpOption.getOpt())) {
            HelpFormatter helpFormatter = new HelpFormatter();
            helpFormatter.printHelp("phylosdp", options);
        } else {
            final String inFileName = commandLine.getOptionValue(inputFileOption.getOpt());
            final String outFileName = commandLine.getOptionValue(outputFileOption.getOpt());
            final String minMinorStrainCountStr = commandLine
                    .getOptionValue(minMinorStrainCountOption.getOpt());
            final int minMinorStrainCount = Integer.parseInt(minMinorStrainCountStr);
            FlatFileReader ffr = new FlatFileReader(new FileReader(inFileName), CommonFlatFileFormat.CSV_UNIX);
            String[] inHeader = ffr.readRow();
            if (inHeader == null) {
                throw new IOException("the input file is empty");
            } else if (inHeader.length != 4) {
                throw new IllegalFormatException(
                        "expected the input to have 4 columns but found " + inHeader.length + " columns");
            } else {
                Map<BitSet, List<GenomeInterval>> sdpMap = new HashMap<BitSet, List<GenomeInterval>>();

                ArrayList<String> strainNames = null;
                String[] outHeader = null;
                String[] currInRow = null;
                while ((currInRow = ffr.readRow()) != null) {
                    GenomeInterval genoInt = new GenomeInterval(currInRow[0], Long.parseLong(currInRow[1]),
                            Long.parseLong(currInRow[2]));
                    PhylogenyTreeNode phylo = PhylogenyTreeNode.fromNewickFormat(currInRow[3]);
                    if (strainNames == null) {
                        strainNames = new ArrayList<String>(phylo.getAllStrains());
                        Collections.sort(strainNames);

                        outHeader = new String[strainNames.size() + 1];
                        for (int i = 0; i < strainNames.size(); i++) {
                            outHeader[i] = strainNames.get(i);
                        }
                        outHeader[outHeader.length - 1] = "genomicIntervals";
                    }

                    Set<BitSet> phyloSdps = phylo.sdps(strainNames, minMinorStrainCount);
                    for (BitSet sdp : phyloSdps) {
                        List<GenomeInterval> sdpIntervals = sdpMap.get(sdp);
                        if (sdpIntervals == null) {
                            sdpIntervals = new ArrayList<GenomeInterval>();
                            sdpMap.put(sdp, sdpIntervals);
                        }
                        sdpIntervals.add(genoInt);
                    }
                }

                if (strainNames == null) {
                    System.out.println("The input file " + inFileName + " appears empty");
                    System.exit(-1);
                } else {
                    // write the header row
                    FlatFileWriter ffw = new FlatFileWriter(
                            //new OutputStreamWriter(System.out),
                            new FileWriter(outFileName), CommonFlatFileFormat.CSV_UNIX);
                    ffw.writeRow(outHeader);
                    int strainCount = strainNames.size();
                    String[] currOutRow = new String[strainNames.size() + 1];
                    for (Map.Entry<BitSet, List<GenomeInterval>> entry : sdpMap.entrySet()) {
                        BitSet currSDP = entry.getKey();
                        for (int i = 0; i < strainCount; i++) {
                            currOutRow[i] = currSDP.get(i) ? "1" : "0";
                        }

                        StringBuilder sb = new StringBuilder();
                        List<GenomeInterval> intervals = entry.getValue();
                        int intervalCount = intervals.size();
                        for (int i = 0; i < intervalCount; i++) {
                            GenomeInterval interval = intervals.get(i);
                            sb.append(interval.getChrId());
                            sb.append(';');
                            sb.append(interval.getStartPositionBp());
                            sb.append(';');
                            sb.append(interval.getEndPositionBp());
                            if (i < intervalCount - 1) {
                                sb.append('|');
                            }
                        }
                        currOutRow[strainCount] = sb.toString();
                        ffw.writeRow(currOutRow);
                    }

                    ffw.flush();
                }
            }
        }
    } catch (ParseException ex) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("phylosdp", options);

        System.exit(-1);
    }
}

From source file:Main.java

static int[] bits2Ints(BitSet bs) {
    int[] temp = new int[bs.size() / 32];

    for (int i = 0; i < temp.length; i++)
        for (int j = 0; j < 32; j++)
            if (bs.get(i * 32 + j))
                temp[i] |= 1 << j;

    return temp;//from ww w. jav  a2s  .  c  om
}

From source file:Main.java

/**
 * Converts a BitSet into an extended binary field
 * used in pack routines. The result is always in the
 * extended format: (16 bytes of length)
 * <br><br>//  w  w  w .  j a va 2 s. c om
 * @param b the BitSet
 * @return binary representation
 */
public static byte[] bitSet2extendedByte(BitSet b) {
    int len = 128;
    byte[] d = new byte[len >> 3];
    for (int i = 0; i < len; i++)
        if (b.get(i + 1))
            d[i >> 3] |= (0x80 >> (i % 8));
    d[0] |= 0x80;
    return d;
}