Example usage for java.util BitSet xor

List of usage examples for java.util BitSet xor

Introduction

In this page you can find the example usage for java.util BitSet xor.

Prototype

public void xor(BitSet set) 

Source Link

Document

Performs a logical XOR of this bit set with the bit set argument.

Usage

From source file:BitOps.java

public static void main(String args[]) {
    BitSet set = new BitSet();
    set.set(1);/*from w w  w .  ja v  a2  s  . c  o m*/
    set.set(2);
    set.set(3);
    set.set(4);
    set.set(5);
    System.out.println(set);
    BitSet set2 = new BitSet();
    set2.set(1);
    set2.set(3);
    set2.set(5);
    set2.set(7);
    BitSet set3 = (BitSet) set.clone();
    set3.and(set2);
    System.out.println(set3);
    set3 = (BitSet) set.clone();
    set3.or(set2);
    System.out.println(set3);
    set3 = (BitSet) set.clone();
    set3.xor(set2);
    System.out.println(set3);
    set3 = (BitSet) set.clone();
    set3.andNot(set2);
    System.out.println(set3);
    set3.andNot(null);
}

From source file:MainClass.java

public static void main(String args[]) {
    BitSet bites = new BitSet();
    bites.set(0);/*from  w w w.j a  va  2 s . co m*/
    bites.set(1);
    bites.set(2);
    bites.set(3);
    System.out.println(bites);

    bites.and(bites);
    System.out.println(bites);

    bites.or(bites);
    System.out.println(bites);

    bites.xor(bites);
    System.out.println(bites);

    bites.andNot(bites);
    System.out.println(bites);

}

From source file:Main.java

public static void main(String[] args) {

    BitSet bitset1 = new BitSet(8);
    BitSet bitset2 = new BitSet(8);

    // assign values to bitset1
    bitset1.set(0);//w  ww.j a  v  a2  s .  co  m
    bitset1.set(1);
    bitset1.set(2);

    // assign values to bitset2
    bitset2.set(2);
    bitset2.set(4);
    bitset2.set(6);

    // print the sets
    System.out.println("Bitset1:" + bitset1);
    System.out.println("Bitset2:" + bitset2);

    // perform a xor operation between two bitsets
    bitset1.xor(bitset2);
    System.out.println(bitset1);
}

From source file:MainClass.java

public static void main(String args[]) {
    BitSet bits1 = new BitSet(16);
    BitSet bits2 = new BitSet(16);

    // set some bits
    for (int i = 0; i < 16; i++) {
        if ((i % 2) == 0)
            bits1.set(i);/* w  w  w  .  ja  va2  s.  c o m*/
        if ((i % 5) != 0)
            bits2.set(i);
    }

    System.out.println("Initial pattern in bits1: ");
    System.out.println(bits1);
    System.out.println("\nInitial pattern in bits2: ");
    System.out.println(bits2);

    // AND bits
    bits2.and(bits1);
    System.out.println("\nbits2 AND bits1: ");
    System.out.println(bits2);

    // OR bits
    bits2.or(bits1);
    System.out.println("\nbits2 OR bits1: ");
    System.out.println(bits2);

    // XOR bits
    bits2.xor(bits1);
    System.out.println("\nbits2 XOR bits1: ");
    System.out.println(bits2);
}

From source file:com.textocat.textokit.postagger.DictionaryComplianceChecker.java

private static int calcDistance(final BitSet xArg, final BitSet yArg) {
    BitSet x = (BitSet) xArg.clone();
    x.xor(yArg);
    return x.cardinality();
}

From source file:com.cloudera.oryx.kmeans.computation.cluster.KSketchIndex.java

private static int hammingDistance(BitSet q, BitSet idx) {
    BitSet x = new BitSet(q.size());
    x.or(q);/*from  w w w  .  j  av  a  2  s  .c  o  m*/
    x.xor(idx);
    return x.cardinality();
}

From source file:org.asoem.greyfish.utils.collect.BitString.java

protected final BitString standardXor(final BitString other) {
    final BitSet bitSetCopy = this.bitSet();
    bitSetCopy.xor(other.bitSet());
    return BitString.create(bitSetCopy, Math.max(size(), other.size()));
}

From source file:org.roaringbitmap.TestRoaringBitmap.java

public void rTest(final int N) {
    System.out.println("rtest N=" + N);
    for (int gap = 1; gap <= 65536; gap *= 2) {
        final BitSet bs1 = new BitSet();
        final RoaringBitmap rb1 = new RoaringBitmap();
        for (int x = 0; x <= N; x += gap) {
            bs1.set(x);/* w w w.  j  av a 2  s  . com*/
            rb1.add(x);
        }
        if (bs1.cardinality() != rb1.getCardinality())
            throw new RuntimeException("different card");
        if (!equals(bs1, rb1))
            throw new RuntimeException("basic  bug");
        for (int offset = 1; offset <= gap; offset *= 2) {
            final BitSet bs2 = new BitSet();
            final RoaringBitmap rb2 = new RoaringBitmap();
            for (int x = 0; x <= N; x += gap) {
                bs2.set(x + offset);
                rb2.add(x + offset);
            }
            if (bs2.cardinality() != rb2.getCardinality())
                throw new RuntimeException("different card");
            if (!equals(bs2, rb2))
                throw new RuntimeException("basic  bug");

            BitSet clonebs1;
            // testing AND
            clonebs1 = (BitSet) bs1.clone();
            clonebs1.and(bs2);
            if (!equals(clonebs1, RoaringBitmap.and(rb1, rb2)))
                throw new RuntimeException("bug and");
            {
                final RoaringBitmap t = rb1.clone();
                t.and(rb2);
                if (!equals(clonebs1, t))
                    throw new RuntimeException("bug inplace and");
                if (!t.equals(RoaringBitmap.and(rb1, rb2))) {
                    System.out.println(t.highLowContainer.getContainerAtIndex(0).getClass().getCanonicalName());
                    System.out.println(RoaringBitmap.and(rb1, rb2).highLowContainer.getContainerAtIndex(0)
                            .getClass().getCanonicalName());

                    throw new RuntimeException("bug inplace and");
                }
            }

            // testing OR
            clonebs1 = (BitSet) bs1.clone();
            clonebs1.or(bs2);

            if (!equals(clonebs1, RoaringBitmap.or(rb1, rb2)))
                throw new RuntimeException("bug or");
            {
                final RoaringBitmap t = rb1.clone();
                t.or(rb2);
                if (!equals(clonebs1, t))
                    throw new RuntimeException("bug or");
                if (!t.equals(RoaringBitmap.or(rb1, rb2)))
                    throw new RuntimeException("bug or");
                if (!t.toString().equals(RoaringBitmap.or(rb1, rb2).toString()))
                    throw new RuntimeException("bug or");

            }
            // testing XOR
            clonebs1 = (BitSet) bs1.clone();
            clonebs1.xor(bs2);
            if (!equals(clonebs1, RoaringBitmap.xor(rb1, rb2))) {
                throw new RuntimeException("bug xor");
            }
            {
                final RoaringBitmap t = rb1.clone();
                t.xor(rb2);
                if (!equals(clonebs1, t))
                    throw new RuntimeException("bug xor");
                if (!t.equals(RoaringBitmap.xor(rb1, rb2)))
                    throw new RuntimeException("bug xor");
            }
            // testing NOTAND
            clonebs1 = (BitSet) bs1.clone();
            clonebs1.andNot(bs2);
            if (!equals(clonebs1, RoaringBitmap.andNot(rb1, rb2))) {
                throw new RuntimeException("bug andnot");
            }
            clonebs1 = (BitSet) bs2.clone();
            clonebs1.andNot(bs1);
            if (!equals(clonebs1, RoaringBitmap.andNot(rb2, rb1))) {
                throw new RuntimeException("bug andnot");
            }
            {
                final RoaringBitmap t = rb2.clone();
                t.andNot(rb1);
                if (!equals(clonebs1, t)) {
                    throw new RuntimeException("bug inplace andnot");
                }
                final RoaringBitmap g = RoaringBitmap.andNot(rb2, rb1);
                if (!equals(clonebs1, g)) {
                    throw new RuntimeException("bug andnot");
                }
                if (!t.equals(g))
                    throw new RuntimeException("bug");
            }
            clonebs1 = (BitSet) bs1.clone();
            clonebs1.andNot(bs2);
            if (!equals(clonebs1, RoaringBitmap.andNot(rb1, rb2))) {
                throw new RuntimeException("bug andnot");
            }
            {
                final RoaringBitmap t = rb1.clone();
                t.andNot(rb2);
                if (!equals(clonebs1, t)) {
                    throw new RuntimeException("bug andnot");
                }
                final RoaringBitmap g = RoaringBitmap.andNot(rb1, rb2);
                if (!equals(clonebs1, g)) {
                    throw new RuntimeException("bug andnot");
                }
                if (!t.equals(g))
                    throw new RuntimeException("bug");
            }
        }
    }
}

From source file:uk.co.infinityapps.rehpic.cipherblockmode.CipherBlockChainingMode.java

private byte[] xorCipherAndPlainText(byte[] cipherText, byte[] plainText) {
    final BitSet cipherTextBitSet = BitSet.valueOf(cipherText);
    final BitSet plainTextBitSet = BitSet.valueOf(plainText);
    cipherTextBitSet.xor(plainTextBitSet);
    return BitUtils.bitSetToByteArray(cipherTextBitSet, cipherText.length);
}

From source file:uk.co.infinityapps.rehpic.round.RoundFunctionImpl.java

public byte[] encrypt(byte[] blockData, final byte[][] subKeys) {
    for (byte[] subKey : subKeys) {
        blockData = mixingPermutator.mix(blockData);
        blockData = permutator.shiftLeft(blockData, subKey);
        final BitSet blockBitSet = BitSet.valueOf(blockData);
        blockBitSet.xor(BitSet.valueOf(subKey));
        blockData = substitutionBox.subtitute(BitUtils.bitSetToByteArray(blockBitSet, blockData.length),
                subKey);/*from w w  w  .j av a 2 s  . c  om*/
    }
    return blockData;
}