'In Java, how does BitSet get range works?
pls help me with below code snippet understanding
/*package whatever //do not write package name here */
import java.io.*;
import java.util.BitSet;
class GFG {
public static void main (String[] args) {
BitSet bs=new BitSet();
bs.set(4);
bs.set(0);
bs.set(3);
bs.set(6);
bs.set(8);
bs.set(10);
bs.set(11);
bs.set(12);
bs.set(14);
BitSet bs2 = bs.get(4,13);
System.out.println(bs+" "+bs2+" "+bs2.get(2)+" "+bs2.get(4)+" "+bs2.get(5)+" "+bs2.get(12)+" "+
bs2.get(14)+" "+bs2.get(15));
}
}
In above code, how does bs2 gets created from using the get(4,13) on bs?
Solution 1:[1]
As the API documentation states bs2 is a copy of the specified range of bs starting with bit 4 and ending with bit 12 (end index is exclusive). In other words a new BitSet is created that is initialized with the bits set in that range in the original one.
Solution 2:[2]
t is true, f is false!!!
bs:
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
tffttftftftttft
100110101011101
bs(4-13)
xxxx1010101110x
cause BitSet bs2 = bs.get(4,13);
so bs2 not need 'x' !!!
so bs2 is :
tftftftttf
1010101110
0, 2, 4, 6, 7, 8 is 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ewramner |
| Solution 2 |
