下面列出了java.util.BitSet#xor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void bitSetResult(String operation, BitSet leftOperand, BitSet rightOperand, int index) {
switch (operation) {
case "AND":
leftOperand.and(rightOperand);
break;
case "OR":
leftOperand.or(rightOperand);
break;
case "XOR":
leftOperand.xor(rightOperand);
break;
case "FLIP":
leftOperand.flip(index);
break;
case "SET":
leftOperand.set(index);
break;
}
}
private void executeCMAX_SET_Task(int currentJob) {
MAX_SET maxSet = null;
for (MAX_SET m : this.maxSet) {
if (m.getAttribute() == currentJob) {
maxSet = m;
break;
}
}
CMAX_SET result = new CMAX_SET(currentJob);
for (BitSet il : maxSet.getCombinations()) {
BitSet inverse = new BitSet();
inverse.set(0, this.numberOfAttributes);
inverse.xor(il);
result.addCombination(inverse);
}
result.finalize();
this.cmaxSet.add(result);
}
/**
* Checks if clade i contains clade j.
*
* @param i - the parent clade
* @param j - the child clade
* @return true, if i contains j
*/
protected boolean containsClade(BitSet i, BitSet j) {
BitSet tmpI = (BitSet) i.clone();
// just set the bits which are either in j but not in i or in i but not
// in j
tmpI.xor(j);
int numberOfBitsInEither = tmpI.cardinality();
// which bits are just in i
tmpI.and(i);
int numberIfBitJustInContaining = tmpI.cardinality();
// if the number of bits just in i is equal to the number of bits just
// in one of i or j
// then i contains j
return numberOfBitsInEither == numberIfBitJustInContaining;
}
public void removeAcl(OzoneAcl acl) throws OMException {
Objects.requireNonNull(acl, "Acl should not be null.");
if (acl.getAclScope().equals(OzoneAcl.AclScope.DEFAULT)) {
defaultAclList.remove(OzoneAcl.toProtobuf(acl));
return;
}
OzoneAclType aclType = OzoneAclType.valueOf(acl.getType().name());
if (getAccessAclMap(aclType).containsKey(acl.getName())) {
BitSet aclRights = getAccessAclMap(aclType).get(acl.getName());
BitSet bits = (BitSet) acl.getAclBitSet().clone();
bits.and(aclRights);
if (bits.equals(ZERO_BITSET)) {
// throw exception if acl doesn't exist.
throw new OMException("Acl [" + acl + "] doesn't exist.",
INVALID_REQUEST);
}
acl.getAclBitSet().and(aclRights);
aclRights.xor(acl.getAclBitSet());
// Remove the acl as all rights are already set to 0.
if (aclRights.equals(ZERO_BITSET)) {
getAccessAclMap(aclType).remove(acl.getName());
}
} else {
// throw exception if acl doesn't exist.
throw new OMException("Acl [" + acl + "] doesn't exist.",
INVALID_REQUEST);
}
}
/**
* Calculate the Simetric Diference of two subsets
*
* @return the Simetric Diference
* @exception Exception if the calculation can not be completed
*/
public int SimetricDiference(Subset subset, BitSet bitset){
BitSet aux =subset.clone ().subset;
aux.xor (bitset);
return aux.cardinality ();
}
public boolean isSuperSet(SSAIdent other) {
BitSet tmp = (BitSet) val.clone();
tmp.or(other.val);
// if or-ing it changed cardinality, then it wasn't a superset.
if (tmp.cardinality() != val.cardinality()) return false;
tmp.xor(other.val);
return (tmp.cardinality() > 0);
}
/** Determine if this set intersects another.
* @param other the other set in question.
*/
public boolean intersects(terminal_set other)
throws internal_error
{
not_null(other);
/* make a copy of the other set */
BitSet copy = (BitSet)other._elements.clone();
/* xor out our values */
copy.xor(this._elements);
/* see if its different */
return !copy.equals(other._elements);
}
/** Determine if this set intersects another.
* @param other the other set in question.
*/
public boolean intersects(terminal_set other)
throws internal_error
{
not_null(other);
/* make a copy of the other set */
BitSet copy = (BitSet)other._elements.clone();
/* xor out our values */
copy.xor(this._elements);
/* see if its different */
return !copy.equals(other._elements);
}
@SuppressWarnings("ReferenceEquality")
boolean isPrefixOf(KeyBitSet other) {
if (other == this) {
return true;
}
if (other.length < this.length) {
return false;
}
BitSet thisBits = (BitSet) this.keyBits.clone();
thisBits.xor(other.keyBits);
int firstSetBitIndex = thisBits.nextSetBit(0);
return (firstSetBitIndex >= this.length) || (firstSetBitIndex == -1);
}
public int hammingDistance(BinVector other) {
//clone the bit set of this object
BitSet xored = (BitSet) bitSet.clone();
//xor (modify) the bit set
xored.xor(other.bitSet);
//return the number of 1's
return xored.cardinality();
}
public String encode(final String str) {
// create binary representationn of input string
String binaryString = stringToBinary(str);
// add the length (in binary number format) of entire encoded string to
// the begging of the encoded string.
// the size of total binaryString
String sizeOfEncodedString = String.valueOf(binaryString.length() / 7);
String stringSizeBinary = "";
// if the size of the encoded string isnt two digits in length then add
// a zero as padding
if (sizeOfEncodedString.length() < 2) {
sizeOfEncodedString = "0".concat(sizeOfEncodedString);
}
for (int i = 2; i > 0; i--) {
stringSizeBinary = stringToBinary(sizeOfEncodedString.substring(
i - 1, i));
binaryString = stringSizeBinary.concat(binaryString);
}
// create a BitSet based on the binary representation
final BitSet nameSet = createBitSet(binaryString);
// xor the BitSet with the key
nameSet.xor(key);
// turn the xor'd BitSet back into a String so it can be written to file
final StringBuilder strBuff = new StringBuilder(str.length() * 7);
for (int i = 0; i < nameSet.size(); i++) {
if (nameSet.get(i)) {
strBuff.append('1');
} else {
strBuff.append('0');
}
}
strBuff.reverse();
return strBuff.toString();
}
/**
* Calculates the hamming distance between this and the other.
* This is not the most efficient implementation: values are copied.
* @param other the other vector.
* @return The hamming distance between this and the other.
*/
public int hammingDistance(BitSetWithID other) {
//clone the bit set of this object
BitSet xored = (BitSet) bitSet.clone();
//xor (modify) the bit set
xored.xor(other.bitSet);
//return the number of 1's
return xored.cardinality();
}
private boolean isVariablePartitioningEqual(final Stack other, final int toSlot) {
// No difference in the symbol boundaries before the toSlot
final BitSet diff = other.getSymbolBoundaryCopy();
diff.xor(symbolBoundary);
return diff.previousSetBit(toSlot - 1) == -1;
}
private boolean isVariablePartitioningEqual(final Stack other, final int toSlot) {
// No difference in the symbol boundaries before the toSlot
final BitSet diff = other.getSymbolBoundaryCopy();
diff.xor(symbolBoundary);
return diff.previousSetBit(toSlot - 1) == -1;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int M = scan.nextInt();
BitSet B1 = new BitSet(N);
BitSet B2 = new BitSet(N);
while (M-- > 0) {
String str = scan.next();
int a = scan.nextInt();
int b = scan.nextInt();
switch (str) {
case "AND":
if (a == 1) {
B1.and(B2);
} else {
B2.and(B1);
}
break;
case "OR":
if (a == 1) {
B1.or(B2);
} else {
B2.or(B1);
}
break;
case "XOR":
if (a == 1) {
B1.xor(B2);
} else {
B2.xor(B1);
}
break;
case "FLIP":
if (a == 1) {
B1.flip(b);
} else {
B2.flip(b);
}
break;
case "SET":
if (a == 1) {
B1.set(b);
} else {
B2.set(b);
}
break;
default:
break;
}
System.out.println(B1.cardinality() + " " + B2.cardinality());
}
scan.close();
}
private boolean isVariablePartitioningEqual(final Stack other, final int toSlot) {
// No difference in the symbol boundaries before the toSlot
final BitSet diff = other.getSymbolBoundaryCopy();
diff.xor(symbolBoundary);
return diff.previousSetBit(toSlot - 1) == -1;
}
private boolean isVariablePartitioningEqual(final Stack other, final int toSlot) {
// No difference in the symbol boundaries before the toSlot
final BitSet diff = other.getSymbolBoundaryCopy();
diff.xor(symbolBoundary);
return diff.previousSetBit(toSlot - 1) == -1;
}
private boolean isVariablePartitioningEqual(final Stack other, final int toSlot) {
// No difference in the symbol boundaries before the toSlot
final BitSet diff = other.getSymbolBoundaryCopy();
diff.xor(symbolBoundary);
return diff.previousSetBit(toSlot - 1) == -1;
}
private boolean isVariablePartitioningEqual(final Stack other, final int toSlot) {
// No difference in the symbol boundaries before the toSlot
final BitSet diff = other.getSymbolBoundaryCopy();
diff.xor(symbolBoundary);
return diff.previousSetBit(toSlot - 1) == -1;
}
/** represents the result of this bit list logically XORred with the other */
public BitList xorred(BitList other) {
BitSet result = asBitSet();
result.xor(other.asBitSet());
return new BitList(result, Math.max(length, other.length));
}