Less words and more code, here the experiment that in my opinion answers the question:
import java.util.BitSet;
public class BitSetExperiment {
public static void main(String[] args) {
BitSet bs0=new BitSet();
BitSet bs1;
System.out.println("created:\tLength,Size bs0: "+bs0.length()+" , "+bs0.size());
bs0.set(15);
System.out.println("set(15):\tLength,Size bs0: "+bs0.length()+" , "+bs0.size());
bs0.set(63);
System.out.println("set(63):\tLength,Size bs0: "+bs0.length()+" , "+bs0.size());
bs0.set(86);
System.out.println("set(86):\tLength,Size bs0: "+bs0.length()+" , "+bs0.size());
bs0.clear(86);
System.out.println("clear(86):\tLength,Size bs0: "+bs0.length()+" , "+bs0.size());
bs0.clear(63);
System.out.println("clear(63):\tLength,Size bs0: "+bs0.length()+" , "+bs0.size());
System.out.println("Cloning to bs1...\n");
bs1=(BitSet)bs0.clone();
System.out.println("Length,Size bs0: "+bs0.length()+" , "+bs0.size());
System.out.println("Length,Size bs1: "+bs1.length()+" , "+bs1.size());
}
}
The output:
created: Length,Size bs0: 0 , 64
set(15): Length,Size bs0: 16 , 64
set(63): Length,Size bs0: 64 , 64
set(86): Length,Size bs0: 87 , 128
clear(86): Length,Size bs0: 64 , 128
clear(63): Length,Size bs0: 16 , 128
Cloning to bs1...
Length,Size bs0: 16 , 64
Length,Size bs1: 16 , 64
Looking at the output, I found two things:
- When you clear the more significant bits the BitSet DOES NOT trim its size, but maybe it is done using another mechanisms more sophisticated as @Boris the Spider suggest in his comment.
- When you clone a BitSet, the original AND ALSO the clone are represented in the smallest possible allocation (in factors of 64 bits)