Example usage for java.math BigInteger shiftLeft

List of usage examples for java.math BigInteger shiftLeft

Introduction

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

Prototype

public BigInteger shiftLeft(int n) 

Source Link

Document

Returns a BigInteger whose value is (this << n) .

Usage

From source file:net.ripe.rpki.commons.crypto.util.Asn1Util.java

/**
 * IPAddress ::= BIT STRING/*from  w w w . java  2s.  c o  m*/
 */
public static IpAddress parseIpAddress(IpResourceType type, ASN1Encodable der, boolean padWithOnes) {
    expect(der, DERBitString.class);
    DERBitString derBitString = (DERBitString) der;

    byte[] bytes = derBitString.getBytes();
    BigInteger value = new BigInteger(1, bytes);
    int usedBits = bytes.length * Byte.SIZE;
    int neededBits = type.getBitSize();
    int padBits = derBitString.getPadBits();

    if (padBits > 0) {
        byte lastByte = bytes[bytes.length - 1];
        byte mask = (byte) ((1 << padBits) - 1);
        Validate.isTrue((lastByte & mask) == 0, "pad bits not zero");
    }

    BigInteger upperBits = value.shiftLeft(neededBits - usedBits);
    BigInteger lowerBits = BigInteger.ZERO;
    if (padWithOnes) {
        lowerBits = BigInteger.ONE.shiftLeft(neededBits - usedBits + padBits).subtract(BigInteger.ONE);
    }

    return (IpAddress) type.fromBigInteger(upperBits.or(lowerBits));
}

From source file:Util.java

public static BigInteger bitwiseGcd(BigInteger u, BigInteger v) {
    if (u.equals(BigInteger.ZERO))
        return v;
    if (v.equals(BigInteger.ZERO))
        return u;

    //      System.out.format("u=%s=%s\nu.getLowestSetBit()=%s\n%s>>%s=%s = %s\n", u, u.toString(2), u.getLowestSetBit(), u, u.getLowestSetBit(), u.shiftRight(u.getLowestSetBit()), u.shiftRight(u.getLowestSetBit()).toString(2));

    int uBit = u.getLowestSetBit();
    int vBit = v.getLowestSetBit();
    int k = (uBit <= vBit ? uBit : vBit);

    while (u.signum() > 0) {
        // first ensure that both are odd
        u = u.shiftRight(u.getLowestSetBit());
        v = v.shiftRight(v.getLowestSetBit());
        if (u.subtract(v).signum() >= 0) {
            u = (u.subtract(v)).shiftRight(1);
        } else {//  w  w w  .j a  va 2s.  c o  m
            v = (v.subtract(u)).shiftRight(1);
        }
    }

    return v.shiftLeft(k);
}

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

public static BigInteger ipv4ToNumber(String ipAddress) {
    BigInteger result = BigInteger.valueOf(0);
    String[] atoms = ipAddress.split("\\.");

    for (int i = 3; i >= 0; i--) {
        BigInteger bi = new BigInteger(atoms[3 - i]);
        result = result.shiftLeft(8).add(bi);
    }/*from w ww  .  j  a  va 2 s . c  o  m*/

    return result;
}

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

public static BigInteger ipv6ToNumber(String addr) {
    int startIndex = addr.indexOf("::");

    if (startIndex != -1) {

        String firstStr = addr.substring(0, startIndex);
        String secondStr = addr.substring(startIndex + 2, addr.length());

        BigInteger first = ipv6ToNumber(firstStr);

        int x = countChar(addr, ':');
        int y = countChar(secondStr, ':');
        //first = first.shiftLeft(16 * (7 - x)).add(ipv6ToNumber(secondStr));
        first = first.shiftLeft(16 * (7 - x + y));
        first = first.add(ipv6ToNumber(secondStr));

        return first;
    }//from w w  w  .jav  a  2  s  . c o m

    String[] strArr = addr.split(":");

    BigInteger retValue = BigInteger.valueOf(0);
    for (int i = 0; i < strArr.length; i++) {
        BigInteger bi = new BigInteger(strArr[i], 16);
        retValue = retValue.shiftLeft(16).add(bi);
    }
    return retValue;
}

From source file:org.rifidi.edge.readerplugin.awid.awid2010.communication.messages.Gen2ReadBlockDataResponse.java

@Override
public TagReadEvent getTagReadEvent() {
    EPCGeneration2Event gen2Event = new EPCGeneration2Event();
    byte[] shiftedmessage = new byte[super.rawmessage.length];
    BigInteger bi = new BigInteger(super.rawmessage);
    BigInteger bi2 = bi.shiftLeft(1);

    shiftedmessage = bi2.toByteArray();//from   w w  w . ja  va 2 s. c o  m
    if (memorybank == 0) {
        logger.debug("Reserved Memory Bank" + new String(Hex.encodeHex(shiftedmessage)));
        //TODO: put bytes in reserved mem bank
    } else if (memorybank == 1) {
        logger.debug("EPC Memory Bank" + new String(Hex.encodeHex(shiftedmessage)));
        //TODO: what if we don't have 96 bits?
        gen2Event.setEPCMemory(new BigInteger(Arrays.copyOfRange(shiftedmessage, 7, 19)), 12 * 8);
    } else if (memorybank == 2) {
        logger.debug("TID Memory Bank " + new String(Hex.encodeHex(shiftedmessage)));

        // TODO: this is not quite correct since it assumes that we have 96
        // bits
        gen2Event.setTIDMemory(new BigInteger(Arrays.copyOfRange(shiftedmessage, 3, 17)), 12 * 8);
    } else if (memorybank == 3) {
        logger.debug("User Memory Bank" + new String(Hex.encodeHex(shiftedmessage)));
        // TODO: insert into user memory
    }

    TagReadEvent tre;
    if (super.rawmessage.length == 22) {
        logger.debug("AntennaID " + super.rawmessage[19]);
        tre = new TagReadEvent(readerID, gen2Event, super.rawmessage[19], timestamp);
    } else {
        logger.warn("No Antenna byte, assuming default value of " + 1);
        tre = new TagReadEvent(readerID, gen2Event, 1, timestamp);
    }
    return tre;
}

From source file:org.rifidi.edge.adapter.awid.awid2010.communication.messages.Gen2ReadBlockDataResponse.java

@Override
public TagReadEvent getTagReadEvent() {
    EPCGeneration2Event gen2Event = new EPCGeneration2Event();
    byte[] shiftedmessage = new byte[super.rawmessage.length];
    BigInteger bi = new BigInteger(super.rawmessage);
    BigInteger bi2 = bi.shiftLeft(1);

    shiftedmessage = bi2.toByteArray();// w  ww  .  jav  a2s  .c  o m
    if (memorybank == 0) {
        logger.debug("Reserved Memory Bank" + new String(Hex.encodeHex(shiftedmessage)));
        //TODO: put bytes in reserved mem bank
    } else if (memorybank == 1) {
        logger.debug("EPC Memory Bank" + new String(Hex.encodeHex(shiftedmessage)));
        //TODO: what if we don't have 96 bits?
        BigInteger epc = new BigInteger(Arrays.copyOfRange(shiftedmessage, 7, 19));
        gen2Event.setEPCMemory(epc, epc.toString(16), 12 * 8);
    } else if (memorybank == 2) {
        logger.debug("TID Memory Bank " + new String(Hex.encodeHex(shiftedmessage)));

        // TODO: this is not quite correct since it assumes that we have 96
        // bits
        gen2Event.setTIDMemory(new BigInteger(Arrays.copyOfRange(shiftedmessage, 3, 17)), 12 * 8);
    } else if (memorybank == 3) {
        logger.debug("User Memory Bank" + new String(Hex.encodeHex(shiftedmessage)));
        // TODO: insert into user memory
    }

    TagReadEvent tre;
    if (super.rawmessage.length == 22) {
        logger.debug("AntennaID " + super.rawmessage[19]);
        tre = new TagReadEvent(readerID, gen2Event, super.rawmessage[19], timestamp);
    } else {
        logger.warn("No Antenna byte, assuming default value of " + 1);
        tre = new TagReadEvent(readerID, gen2Event, 1, timestamp);
    }
    return tre;
}

From source file:UInt64.java

/**
 * Create a UInt64 from two longs.//from   w ww  .j ava2  s  .  c  o m
 * 
 * @param top
 *          Most significant 4 bytes.
 * @param bottom
 *          Least significant 4 bytes.
 */
public UInt64(long top, long bottom) {
    BigInteger a = new BigInteger("" + top);
    a = a.shiftLeft(32);
    a = a.add(new BigInteger("" + bottom));
    // if (0 > a.compareTo(BigInteger.ZERO)) { throw new NumberFormatException(MessageFormat.format(
    //    _("{0} is not between {1} and {2}."), new Object[] { a, MIN_VALUE, MAX_BIG_VALUE })); }
    //if (0 < a.compareTo(MAX_BIG_VALUE)) { throw new NumberFormatException(MessageFormat.format(
    //   _("{0} is not between {1} and {2}."), new Object[] { a, MIN_VALUE, MAX_BIG_VALUE })); }
    value = a;
    this.top = top;
    this.bottom = bottom;
}

From source file:com.buildabrand.gsb.util.URLUtils.java

private String convertIpAddress(String ipAddr) {
    String[] ipAddrSplit = StringUtils.split(ipAddr, '.');

    if (ipAddrSplit.length == 0 || ipAddrSplit.length > 4) {
        return null;
    }//  www  .  j a  va  2s .c  o m

    // Basically we should parse octal if we can, but if there are illegal octal
    // numbers, i.e. 08 or 09, then we should just look at decimal and hex.
    boolean allowOctal = !FIND_BAD_OCTAL_REGEXP.matcher(ipAddr).find();

    BigInteger ipNumeric = BigInteger.ZERO;
    int i = 0;
    while (i < ipAddrSplit.length - 1) {
        ipNumeric = ipNumeric.shiftLeft(8);
        BigInteger componentBigInt = convertComponent(ipAddrSplit[i], allowOctal);
        if (componentBigInt == null) {
            return null;
        }

        ipNumeric = ipNumeric.add(componentBigInt);
        i++;
    }
    while (i < 4) {
        ipNumeric = ipNumeric.shiftLeft(8);
        i++;
    }
    BigInteger componentBigInt = convertComponent(ipAddrSplit[ipAddrSplit.length - 1], allowOctal);
    if (componentBigInt == null) {
        return null;
    }
    ipNumeric = ipNumeric.add(componentBigInt);

    return InetAddresses.fromInteger((ipNumeric.intValue())).getHostAddress();
}

From source file:Bytes.java

public BigInteger convertFrom(BigInteger originalAmount, Bytes originalType) {
    /**//from w w  w .j  a  va 2  s  . co  m
     * So, B.convertFrom(BigInteger.ONE, YB)
     * should be used to convert 1 yottabyte to bytes...
     */

    BigInteger currentAmount = originalAmount;
    int convertTo = ordinal();
    int convertFrom = originalType.ordinal();
    while (convertTo != convertFrom) {
        if (convertTo < convertFrom) {
            currentAmount = currentAmount.shiftLeft(10);
            convertFrom--;
        } else {
            currentAmount = currentAmount.shiftRight(10);
            convertFrom++;
        }
    }
    return currentAmount;
}

From source file:de.decoit.visa.net.IPAddress.java

/**
 * Return a {@link java.math.BigInteger BigInteger} object containing the
 * bit mask of this address./*  w ww . j  a  v  a2 s .c  o  m*/
 *
 * @return Bit mask of this address
 */
BigInteger toBigIntBitmask() {
    BigInteger rv = BigInteger.ZERO;

    for (int i = 0; i < ipAddressGroups.length; i++) {
        rv = rv.add(BigInteger.valueOf(ipAddressGroups[i]));

        if (i < ipAddressGroups.length - 1) {
            rv = rv.shiftLeft(ipVersion.getSegmentBitCount());
        }
    }

    return rv;
}