• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

BitSet class

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi everybody,

Can somebody explain BitSet class with an example. Why is it not efficient for large size? What do we use in that case?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the newer versions of Java List and Set are available and implement the new and improved Collections Interface.
Also HashTable and Map are available.
This is an excerpt from: http://developer.java.sun.com/developer/Books/MasteringJava/Ch17/index.html#Vectors
Vectors, Stacks, and Enumeration
The historic collection classes have existed since the beginning of Java's time. While there is nothing technically wrong with them, their usage should be limited to the maintenance of Java 1.0 and 1.1 programs. The primary, historic collection classes that provide ordered access are Vector and Stack. The interface Enumeration offers the means to step through the contents.
Bit Sets
Class BitSet implements a set of bits or a vector of boolean values. Unlike many of the other bit set classes or language features in other languages, this bit set has no limits. You can therefore go way beyond the typical 32- or 256-bit limits imposed by other implementations. First, let's look at the class definition:
public class BitSet
implements Cloneable, Serializable {
public BitSet();
public BitSet(int nbits);
public void and(BitSet set);
public void andNot(BitSet set);
public void clear(int bitIndex);
public Object clone();
public boolean equals(Object obj);
public boolean get(int bitIndex);
public int hashCode();
public int length();
public void or(BitSet set);
public void set(int bitIndex);
public int size();
public String toString ();
public void xor(BitSet set);
}
BitSet operations include the following:
Setting, clearing, and getting single bits
Anding, and notting, oring, and xoring bit sets together
Comparing bit sets
The following program demonstrates their usage, by placing in a BitSet whether or not a planet has an even number of moons. Normally, you would store much larger groups of bits in a BitSet to truly save space:
import java.util.*;
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? " + bits.get(i));
}
}
}

--------------------------------------------------------------------------------
NOTE:
Although BitSet is part of the historical collection classes, the new Collections framework has no replacement. Also, the size() method of BitSet returns the size of the internal structure used, not necessarily the maximum bit number utilized. Since the bit values are stored with the help of a double, size() jumps up by 64 when necessary. More frequently you want the length() method, which returns the highest set bit position.
 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Cindy,
Could you explain the size() method of BitSet class in more detail? What does it mean by "the size() method of BitSet returns the size of the internal structure used, not necessarily the maximum bit number utilized. Since the bit values are stored with the help of a double, size() jumps up by 64 when necessary" ?
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure,
If you have a BitSet with one bit number utilized, the internal data structure is 64 because a double data structure is used. When you bump the BitSet up to 2 bits the data structure stays at 64 because there is room.
When you have filled the 64 available bits and try for a 65th bit, the internal data structure jumps to 128 by adding another double.
This really messes up the size() concept because as a rule when using that method you are looking for the number of bits used, and instead you get the size of the data structure currently in place.
 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks a lot, Cindy for clearing my doubts.
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic