Example usage for java.math BigInteger shiftRight

List of usage examples for java.math BigInteger shiftRight

Introduction

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

Prototype

public BigInteger shiftRight(int n) 

Source Link

Document

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

Usage

From source file:Main.java

public static byte[] generateJSF(BigInteger g, BigInteger h) {
    int digits = Math.max(g.bitLength(), h.bitLength()) + 1;
    byte[] jsf = new byte[digits];

    BigInteger k0 = g, k1 = h;
    int j = 0, d0 = 0, d1 = 0;

    int offset = 0;
    while ((d0 | d1) != 0 || k0.bitLength() > offset || k1.bitLength() > offset) {
        int n0 = ((k0.intValue() >>> offset) + d0) & 7, n1 = ((k1.intValue() >>> offset) + d1) & 7;

        int u0 = n0 & 1;
        if (u0 != 0) {
            u0 -= (n0 & 2);//from   ww w . ja va  2s.com
            if ((n0 + u0) == 4 && (n1 & 3) == 2) {
                u0 = -u0;
            }
        }

        int u1 = n1 & 1;
        if (u1 != 0) {
            u1 -= (n1 & 2);
            if ((n1 + u1) == 4 && (n0 & 3) == 2) {
                u1 = -u1;
            }
        }

        if ((d0 << 1) == 1 + u0) {
            d0 ^= 1;
        }
        if ((d1 << 1) == 1 + u1) {
            d1 ^= 1;
        }

        if (++offset == 30) {
            offset = 0;
            k0 = k0.shiftRight(30);
            k1 = k1.shiftRight(30);
        }

        jsf[j++] = (byte) ((u0 << 4) | (u1 & 0xF));
    }

    // Reduce the JSF array to its actual length
    if (jsf.length > j) {
        jsf = trim(jsf, j);
    }

    return jsf;
}

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;
    }// w w w  . j  ava 2s. c  o 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;
}

From source file:Main.java

public static byte[] generateJSF(BigInteger g, BigInteger h) {
    byte[] jsf = new byte[(Math.max(g.bitLength(), h.bitLength()) + 1)];
    BigInteger k0 = g;
    BigInteger k1 = h;/*from  w w w  .ja  v a  2s  .  c  o m*/
    int d0 = 0;
    int d1 = 0;
    int offset = 0;
    int j = 0;
    while (true) {
        if ((d0 | d1) == 0 && k0.bitLength() <= offset && k1.bitLength() <= offset) {
            break;
        }
        int n0 = ((k0.intValue() >>> offset) + d0) & 7;
        int n1 = ((k1.intValue() >>> offset) + d1) & 7;
        int u0 = n0 & 1;
        if (u0 != 0) {
            u0 -= n0 & 2;
            if (n0 + u0 == 4 && (n1 & 3) == 2) {
                u0 = -u0;
            }
        }
        int u1 = n1 & 1;
        if (u1 != 0) {
            u1 -= n1 & 2;
            if (n1 + u1 == 4 && (n0 & 3) == 2) {
                u1 = -u1;
            }
        }
        if ((d0 << 1) == u0 + 1) {
            d0 ^= 1;
        }
        if ((d1 << 1) == u1 + 1) {
            d1 ^= 1;
        }
        offset++;
        if (offset == 30) {
            offset = 0;
            k0 = k0.shiftRight(30);
            k1 = k1.shiftRight(30);
        }
        int j2 = j + 1;
        jsf[j] = (byte) ((u0 << 4) | (u1 & 15));
        j = j2;
    }
    if (jsf.length > j) {
        return trim(jsf, j);
    }
    return jsf;
}

From source file:com.ok2c.lightmtp.util.InetAddressRange.java

public boolean contains(final InetAddress ip) {
    BigInteger bigint2 = new BigInteger(ip.getAddress());
    if (this.shiftBy > 0) {
        bigint2 = bigint2.shiftRight(this.shiftBy);
    }//w  w  w  . j  a  va  2s .c om
    return this.bigint.compareTo(bigint2) == 0;
}

From source file:net.onrc.openvirtex.util.OVXFlowManager.java

public LinkedList<MACAddress> getFlowValues(final Integer flowId) {
    final LinkedList<MACAddress> macList = new LinkedList<MACAddress>();
    final BigInteger dualMac = this.flowValues.get(flowId);
    if (dualMac != null) {
        final MACAddress srcMac = MACAddress.valueOf(dualMac.shiftRight(48).longValue());
        final MACAddress dstMac = MACAddress.valueOf(dualMac.longValue());
        macList.add(srcMac);/*from  w w w  .  ja va2 s  .  c  o m*/
        macList.add(dstMac);
    }
    return macList;
}

From source file:com.ok2c.lightmtp.util.InetAddressRange.java

public InetAddressRange(final InetAddress address, final int mask) {
    super();//  ww w . j a v  a  2s  .co m
    Args.notNull(address, "Address");
    if (mask < 0) {
        throw new IllegalArgumentException("Address mask may not be negative");
    }
    this.address = address;
    this.mask = mask;

    byte[] addr = address.getAddress();
    BigInteger bigint = new BigInteger(addr);
    int shiftBy = 0;
    if (mask > 0) {
        if (addr.length == 4) {
            shiftBy = 32 - mask;
        } else if (addr.length == 16) {
            shiftBy = 128 - mask;
        } else {
            throw new IllegalArgumentException("Unsupported address: " + address);
        }
    }
    if (shiftBy > 0) {
        bigint = bigint.shiftRight(shiftBy);
    }
    this.bigint = bigint;
    this.shiftBy = shiftBy;
}

From source file:Bytes.java

public BigInteger convertFrom(BigInteger originalAmount, Bytes originalType) {
    /**/*w w  w . j a va  2 s  . c  o  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:cc.redberry.core.number.Complex.java

@Override
public Complex pow(BigInteger exponent) {
    if (exponent.compareTo(BigInteger.ZERO) < 0)
        return reciprocal().pow(exponent.negate());

    Complex result = Complex.ONE;/*from w  ww  . j a v a  2 s  .c  o  m*/
    Complex k2p = this;
    while (!BigInteger.ZERO.equals(exponent)) {
        if (exponent.testBit(0))
            result = result.multiply(k2p);

        k2p = k2p.multiply(k2p);
        exponent = exponent.shiftRight(1);
    }

    return result;
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jRefreshAddressTranslateButtonActionPerformed(ActionEvent evt) {
    AddressTranslateTableModel model = (AddressTranslateTableModel) this.jAddressTranslateTable2.getModel();

    if (jSearchAddressRadioButton1.isSelected()) {
        if (!this.jAddressTextField.getText().contains(":")
                || this.jAddressTextField.getText().replaceAll("[^:]", "").length() != 1) {
            JOptionPane.showMessageDialog(this,
                    "Error, please input <segment selector>:<offset>\n\ne.g. : 0x10:0x12345678", "Error",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }/*from www  .  j  a  v a2  s. c  o m*/
        BigInteger segSelector = CommonLib.string2BigInteger(this.jAddressTextField.getText().split(":")[0]);
        BigInteger address = CommonLib.string2BigInteger(this.jAddressTextField.getText().split(":")[1]);

        // for (int x = 0; x < model.getRowCount(); x++) {
        // if (model.searchType.get(x).equals(1) &&
        // model.searchSegSelector.get(x).equals(segSelector) &&
        // model.searchAddress.get(x).equals(address)) {
        // return;
        // }
        // }

        model.searchType.add(1);
        model.searchSegSelector.add(segSelector);
        model.searchAddress.add(address);

        model.virtualAddress.add(address);
        BigInteger segNo = segSelector.shiftRight(3);
        model.segNo.add(segNo);

        // read GDT descriptor
        int descriptor[] = PeterBochsCommonLib
                .getMemoryFromBochs(CommonLib.string2BigInteger(this.registerPanel.jGDTRTextField.getText())
                        .add(segNo.multiply(BigInteger.valueOf(8))), 8);
        BigInteger baseAddress = CommonLib.getBigInteger(descriptor[2], descriptor[3], descriptor[4],
                descriptor[7], 0, 0, 0, 0);
        BigInteger linearAddress = baseAddress.add(address);
        model.baseAddress.add(baseAddress);
        model.linearAddress.add(linearAddress);

        BigInteger pdNo = CommonLib.getBigInteger(linearAddress, 31, 22);
        model.pdNo.add(pdNo);
        int pdeBytes[] = PeterBochsCommonLib
                .getMemoryFromBochs(CommonLib.string2BigInteger(this.registerPanel.jCR3TextField.getText())
                        .add(pdNo.multiply(BigInteger.valueOf(4))), 4);
        BigInteger pde = CommonLib.getBigInteger(pdeBytes, 0);
        model.pde.add(pde);

        BigInteger ptNo = CommonLib.getBigInteger(linearAddress, 21, 12);
        model.ptNo.add(ptNo);
        BigInteger pageTableBaseAddress = pde.and(CommonLib.string2BigInteger("0xfffff000"));
        int pteBytes[] = PeterBochsCommonLib
                .getMemoryFromBochs(pageTableBaseAddress.add(ptNo.multiply(BigInteger.valueOf(4))), 4);
        BigInteger pte = CommonLib.getBigInteger(pteBytes, 0);
        BigInteger pagePhysicalAddress = pte.and(CommonLib.string2BigInteger("0xfffff000"));
        model.pte.add(pte);

        BigInteger physicalAddress = pagePhysicalAddress.add(CommonLib.getBigInteger(linearAddress, 11, 0));
        model.physicalAddress.add(physicalAddress);
        int bytesAtPhysicalAddress[] = PeterBochsCommonLib.getMemoryFromBochs(physicalAddress, 8);
        model.bytes.add(PeterBochsCommonLib.convertToString(bytesAtPhysicalAddress));

        model.fireTableDataChanged();
    } else if (jSearchAddressRadioButton2.isSelected()) {
        // for (int x = 0; x < model.getRowCount(); x++) {
        // if (model.searchType.get(x).equals(2) &&
        // model.searchAddress.get(x).equals(CommonLib.string2long(this.jAddressTextField.getText())))
        // {
        // return;
        // }
        // }
        BigInteger address = CommonLib.string2BigInteger(this.jAddressTextField.getText());

        model.searchType.add(2);
        model.searchAddress.add(address);

        BigInteger baseAddress = BigInteger.ZERO;
        BigInteger linearAddress = baseAddress.add(address);
        model.baseAddress.add(baseAddress);
        model.linearAddress.add(linearAddress);

        BigInteger pdNo = CommonLib.getBigInteger(linearAddress, 31, 22);
        model.pdNo.add(pdNo);
        int pdeBytes[] = PeterBochsCommonLib
                .getMemoryFromBochs(CommonLib.string2BigInteger(this.registerPanel.jCR3TextField.getText())
                        .add(pdNo.multiply(BigInteger.valueOf(4))), 4);
        BigInteger pde = CommonLib.getBigInteger(pdeBytes, 0);
        model.pde.add(pde);

        BigInteger ptNo = CommonLib.getBigInteger(linearAddress, 21, 12);
        model.ptNo.add(ptNo);
        BigInteger pageTableBaseAddress = pde.and(CommonLib.string2BigInteger("0xfffff000"));
        int pteBytes[] = PeterBochsCommonLib
                .getMemoryFromBochs(pageTableBaseAddress.add(ptNo.multiply(BigInteger.valueOf(4))), 4);
        BigInteger pte = CommonLib.getBigInteger(pteBytes, 0);
        BigInteger pagePhysicalAddress = pte.and(CommonLib.string2BigInteger("0xfffff000"));
        model.pte.add(pte);

        BigInteger physicalAddress = pagePhysicalAddress.add(CommonLib.getBigInteger(linearAddress, 11, 0));
        model.physicalAddress.add(physicalAddress);
        int bytesAtPhysicalAddress[] = PeterBochsCommonLib.getMemoryFromBochs(physicalAddress, 8);
        model.bytes.add(PeterBochsCommonLib.convertToString(bytesAtPhysicalAddress));

        model.fireTableDataChanged();
    } else if (jSearchAddressRadioButton3.isSelected()) {
        for (int x = 0; x < model.getRowCount(); x++) {
            if (model.searchType.get(x).equals(3) && model.searchAddress.get(x)
                    .equals(CommonLib.string2long(this.jAddressTextField.getText()))) {
                return;
            }
        }
        BigInteger addr = CommonLib.string2BigInteger(this.jAddressTextField.getText());
        model.searchType.add(3);
        model.searchSegSelector.add(BigInteger.ZERO);
        model.searchAddress.add(addr);
        model.virtualAddress.add(BigInteger.ZERO);
        model.segNo.add(BigInteger.ZERO);
        model.linearAddress.add(BigInteger.ZERO);
        model.pdNo.add(BigInteger.ZERO);
        model.ptNo.add(BigInteger.ZERO);
        model.physicalAddress.add(BigInteger.ZERO);
        model.bytes.add("");

        model.fireTableDataChanged();
    }
}

From source file:net.spfbl.whois.SubnetIPv6.java

public static String getNextIPv6(String ip) {
    if (ip == null) {
        return null;
    } else if (SubnetIPv6.isValidIPv6(ip)) {
        BigInteger address = new BigInteger(1, address(ip));
        if (address.equals(ADDRESS_MAX)) {
            return null;
        } else {/*from w ww .  j  a va2 s  . c o m*/
            address = address.add(ADDRESS_UNIT);
            int p8 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p7 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p6 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p5 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p4 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p3 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p2 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p1 = address.and(ADDRESS_OCTET).intValue();
            return Integer.toHexString(p1) + ":" + Integer.toHexString(p2) + ":" + Integer.toHexString(p3) + ":"
                    + Integer.toHexString(p4) + ":" + Integer.toHexString(p5) + ":" + Integer.toHexString(p6)
                    + ":" + Integer.toHexString(p7) + ":" + Integer.toHexString(p8);
        }
    } else {
        return null;
    }
}