我正在使用Java中的BitSet类来处理一组位。
在比较两个BitSet时,我需要清楚地区分“子集”和“交集”的概念。
让我们通过AND运算符的示例来获取子集:
让我们通过AND运算符的示例来获取子集:
BitSet bits1 = new BitSet();
BitSet bits2 = new BitSet();
bits1.set(0,2,true); //110
bits2.set(1); //010
//010 is a SUBSET of 110
bits1.and(bits2); //bits1 became the result of the and operator
if(bits1.equals(bits2))
{
System.out.println(bits2 + " is a subset of " + bits1);
}
//PRINT
BitSet bits4 = new BitSet();
bits4.set(0,2,true); //110
BitSet bits3 = new BitSet();
bits3.set(1,3,true); //011
bits4.and(bits3);
//011 is NOT a subset of 110
if(bits4.equals(bits3))
{
System.out.println(bits4 + " is a subset of " + bits3);
}
//NO PRINT
由于我使用AND运算符来验证BitSet是否是另一个的子集,因此子集相当明确。
内置交集运算符的同一示例:
BitSet bits1 = new BitSet();
BitSet bits2 = new BitSet();
bits1.set(0,2,true); //110
bits2.set(1); //010
//010 intersect 110, but is also a subset of 110
System.out.println("Intersection? " + bits2.intersects(bits1));
BitSet bits3 = new BitSet();
bits3.set(1,3,true); //011
//011 VS 110 intersection only
System.out.println("Intersection? " + bits3.intersects(bits1));
我的问题是:运算符“交集(intersection)”同时检测子集和相交部分。 我的目标是仅检测相交部分,排除那些也是子集的相交内容,例如第二个示例中的bits1与bits2。因此,这个运算符对我的情况不太适用,因为太过笼统了。 有没有一种方法可以检测这个属性?