Java Code Examples for java.util.BitSet#xor()

The following examples show how to use java.util.BitSet#xor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractCladeImportanceDistribution.java    From beast-mcmc with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: JavaBitSet.java    From Hacktoberfest with MIT License 6 votes vote down vote up
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;
	}
}
 
Example 3
Source File: CMAX_SET_Generator.java    From metanome-algorithms with Apache License 2.0 6 votes vote down vote up
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);
    }
 
Example 4
Source File: BitSetWithID.java    From TarsosLSH with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 5
Source File: ScatterSearchV1.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
* 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 ();
 }
 
Example 6
Source File: Encoder.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 7
Source File: SSAIdent.java    From cfr with MIT License 5 votes vote down vote up
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);
}
 
Example 8
Source File: terminal_set.java    From vtd-xml with GNU General Public License v2.0 5 votes vote down vote up
/** 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);
  }
 
Example 9
Source File: OmOzoneAclMap.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 10
Source File: terminal_set.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/** 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);
  }
 
Example 11
Source File: KeyBitSet.java    From exonum-java-binding with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 12
Source File: BinVector.java    From TarsosLSH with GNU Lesser General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 13
Source File: Label.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 14
Source File: BitList.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
/** 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));
}
 
Example 15
Source File: Label.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 16
Source File: Label.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 17
Source File: Label.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 18
Source File: soln.java    From HackerRank-solutions with MIT License 4 votes vote down vote up
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();
}
 
Example 19
Source File: Label.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 20
Source File: Label.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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;
}