Manipulating the BitSet : BitSet « Collections Data Structure « Java






Manipulating the BitSet

Manipulating the BitSet
  
import java.util.BitSet;

public class TwoBitPlanets {
  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);
      }
    }
    for (int i = 0; i < namesLen; i++) {
      System.out.println(names[i] + " Even # Moons (" + moons[i] + ")? "
          + bits.get(i));
    }
  }
}
           
         
    
  








Related examples in the same category

1.Java 1.5 (5.0) Changes to the API: several of the new bit manipulation methods in Integer.Java 1.5 (5.0) Changes to the API: several of the new bit manipulation methods in Integer.
2.The use of BitSetThe use of BitSet
3.Another Bitset demoAnother Bitset demo
4.Operations on series of numbers
5.BitOHoney
6.BitOps
7.Convert bitset to int array and string
8.Implementation of a bit map of any size, together with static methods to manipulate int, byte and byte[] values as bit maps
9.Operations on bit-mapped fields.