Example usage for java.math BigInteger xor

List of usage examples for java.math BigInteger xor

Introduction

In this page you can find the example usage for java.math BigInteger xor.

Prototype

public BigInteger xor(BigInteger val) 

Source Link

Document

Returns a BigInteger whose value is (this ^ val) .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 };
    BigInteger bi = new BigInteger(bytes);

    bi = bi.xor(bi);
}

From source file:Main.java

public static void main(String[] args) {

    BigInteger bi1 = new BigInteger("8");
    BigInteger bi2 = new BigInteger("-6");

    // perform xor on bi1, bi2 and assign result to bi3
    BigInteger bi3 = bi1.xor(bi2);

    System.out.println(bi3);/*from w  ww  . ja  va  2 s.com*/
}

From source file:Main.java

public static int getNafWeight(BigInteger k) {
    if (k.signum() == 0) {
        return 0;
    }//from   w w  w.  j  a v  a2  s.com

    BigInteger _3k = k.shiftLeft(1).add(k);
    BigInteger diff = _3k.xor(k);

    return diff.bitCount();
}

From source file:Main.java

public static BigInteger hashFnv1A(byte[] data) {
    final BigInteger INIT64 = new BigInteger("cbf29ce484222325", 16);
    final BigInteger PRIME64 = new BigInteger("100000001b3", 16);
    final BigInteger MOD64 = new BigInteger("2").pow(64);

    BigInteger hash = INIT64;

    for (byte b : data) {
        hash = hash.xor(BigInteger.valueOf((int) b & 0xff));
        hash = hash.multiply(PRIME64).mod(MOD64);
    }/* w w  w  .j av  a2s.co m*/

    return hash;
}

From source file:Main.java

public static byte[] generateNaf(BigInteger k) {
    if (k.signum() == 0) {
        return EMPTY_BYTES;
    }/*from   w w  w  . j  av  a2 s.  c  o  m*/

    BigInteger _3k = k.shiftLeft(1).add(k);

    int digits = _3k.bitLength() - 1;
    byte[] naf = new byte[digits];

    BigInteger diff = _3k.xor(k);

    for (int i = 1; i < digits; ++i) {
        if (diff.testBit(i)) {
            naf[i - 1] = (byte) (k.testBit(i) ? -1 : 1);
            ++i;
        }
    }

    naf[digits - 1] = 1;

    return naf;
}

From source file:com.wirecard.ezlinkwebservices.util.TerminalUtil.java

public static synchronized String strXor(String s1, String s2) {
    System.out.println("strXor=" + s1 + "  " + s2);
    BigInteger one = new BigInteger(s1, 16);
    BigInteger two = new BigInteger(s2, 16);
    BigInteger three = one.xor(two);
    String s3 = three.toString(16);
    System.out.println(s3);/*from ww  w . j  av  a2 s .  c  o m*/
    return s3;
}

From source file:Main.java

public static int[] generateCompactNaf(BigInteger k) {
    if ((k.bitLength() >>> 16) != 0) {
        throw new IllegalArgumentException("'k' must have bitlength < 2^16");
    }/*from  w  ww  . jav a 2s  .com*/
    if (k.signum() == 0) {
        return EMPTY_INTS;
    }

    BigInteger _3k = k.shiftLeft(1).add(k);

    int bits = _3k.bitLength();
    int[] naf = new int[bits >> 1];

    BigInteger diff = _3k.xor(k);

    int highBit = bits - 1, length = 0, zeroes = 0;
    for (int i = 1; i < highBit; ++i) {
        if (!diff.testBit(i)) {
            ++zeroes;
            continue;
        }

        int digit = k.testBit(i) ? -1 : 1;
        naf[length++] = (digit << 16) | zeroes;
        zeroes = 1;
        ++i;
    }

    naf[length++] = (1 << 16) | zeroes;

    if (naf.length > length) {
        naf = trim(naf, length);
    }

    return naf;
}

From source file:com.wms.utils.DataUtil.java

public static boolean checkValidateIPv4(String fromIPAddress, String toIPAddress, int mask) {

    BigInteger fromIP = ipv4ToNumber(fromIPAddress);
    BigInteger toIP = ipv4ToNumber(toIPAddress);
    BigInteger subnet = new BigInteger("FFFFFFFF", 16);

    fromIP = fromIP.shiftRight(32 - mask).shiftLeft(32 - mask);
    subnet = subnet.shiftRight(mask);/* ww  w .  ja  v  a 2s. com*/

    BigInteger broadcastIP = fromIP.xor(subnet);

    if (toIP.compareTo(broadcastIP) == 1) {
        return false;
    }

    return true;
}

From source file:org.springframework.cloud.consul.bus.EventService.java

/**
 * from https://github.com/hashicorp/consul/blob/master/api/event.go#L90-L104 //
 * IDToIndex is a bit of a hack. This simulates the index generation to // convert an
 * event ID into a WaitIndex. func (e *Event) IDToIndex(uuid string) uint64 { lower :=
 * uuid[0:8] + uuid[9:13] + uuid[14:18] upper := uuid[19:23] + uuid[24:36] lowVal, err
 * := strconv.ParseUint(lower, 16, 64) if err != nil { panic("Failed to convert " +
 * lower) } highVal, err := strconv.ParseUint(upper, 16, 64) if err != nil {
 * panic("Failed to convert " + upper) } return lowVal ^ highVal //^ bitwise XOR
 * integers }//from w  w w . j a v  a  2s.  c o  m
 */
public BigInteger toIndex(String eventId) {
    String lower = eventId.substring(0, 8) + eventId.substring(9, 13) + eventId.substring(14, 18);
    String upper = eventId.substring(19, 23) + eventId.substring(24, 36);
    BigInteger lowVal = new BigInteger(lower, 16);
    BigInteger highVal = new BigInteger(upper, 16);
    BigInteger index = lowVal.xor(highVal);
    return index;
}

From source file:com.wms.utils.DataUtil.java

public static boolean checkValidateIPv6(String fromIPAddress, String toIPAddress, int mask) {

    BigInteger fromIP = ipv6ToNumber(fromIPAddress);
    BigInteger toIP = ipv6ToNumber(toIPAddress);
    BigInteger limit = toIP.subtract(fromIP);
    if (limit.compareTo(new BigInteger(MAX_NUMBER_RANGE)) == 1) {
        return false;
    }//from   w ww . j a v a  2  s  .  co m
    BigInteger subnet = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);

    fromIP = fromIP.shiftRight(128 - mask).shiftLeft(128 - mask);
    subnet = subnet.shiftRight(mask);

    BigInteger broadcastIP = fromIP.xor(subnet);

    if (toIP.compareTo(broadcastIP) == 1) {
        return false;
    }

    return true;
}