List of usage examples for java.math BigInteger add
BigInteger add(long val)
From source file:Main.java
public static BigInteger getZ(ArrayList<byte[]> c1, ArrayList<byte[]> c2, BigInteger p) { BigInteger z = BigInteger.ZERO; //TODO: make sure c1 and c2 are of the same size int size = c1.size(); if (size > c2.size()) { size = c2.size();/*from w w w. ja v a 2 s. c o m*/ } for (int i = 0; i < size; i++) { BigInteger c1BI = new BigInteger(1, c1.get(i)); BigInteger c2BI = new BigInteger(1, c2.get(i)); BigInteger exp = new BigInteger(1, ByteBuffer.allocate(8).putLong((long) Math.pow(2, i)).array()); z = z.add((c1BI.multiply(c2BI)).modPow(exp, p)); Log.d("CeCk", "z calculation " + i + "/" + size + " round"); } return z.mod(p); }
From source file:jp.co.ntts.vhut.util.Ipv4ConversionUtil.java
/** * ??IP??NW??????./*from w w w. ja va 2s . com*/ * @param bAddr IP? * @param bMask (?) * @return ?? */ public static byte[] getNextNetworkAddressAsByte(byte[] bAddr, byte[] bMask) { BigInteger current = new BigInteger(getNetworkAddressAsByte(bAddr, bMask)); BigInteger reversedMask = new BigInteger(byteXOR(bMask, getFullBytes())); return current.add(reversedMask).add(BigInteger.ONE).toByteArray(); }
From source file:jp.co.ntts.vhut.util.Ipv4ConversionUtil.java
/** * IP(HEX)????./*from w ww .ja v a 2 s . co m*/ * ????int????????????. * @param startIpaddr (HEX/) * @param endIpaddr (HEX/) * @return IP(HEX)?? */ public static Set<String> getIpAddressSetBetween(String startIpaddr, String endIpaddr) { BigInteger startBI = new BigInteger(addrTobyte(startIpaddr)); BigInteger endBI = new BigInteger(addrTobyte(endIpaddr)); int length = (int) Math.min(endBI.subtract(startBI).longValue(), Integer.MAX_VALUE); Set<String> resultSet = new HashSet<String>(); BigInteger currentBI = startBI; for (int i = 0; i <= length; i++) { resultSet.add(byteToAddr(currentBI.toByteArray())); currentBI = currentBI.add(BigInteger.ONE); } return resultSet; }
From source file:org.objectspace.rfid.elatec.ElatecRFID.java
public static String longToHex(long l) { BigInteger b = BigInteger.valueOf(l); if (b.signum() < 0) b = b.add(TWO_64); String str = b.toString(16);//from w ww.j a v a 2 s.co m if (str.length() == 16) str = str.substring(8); while (str.length() < 8) str = "0" + str; return str; }
From source file:org.objectspace.rfid.elatec.ElatecRFID.java
public static String intToHex(int i) { BigInteger b = BigInteger.valueOf(i); if (b.signum() < 0) b = b.add(TWO_64); String str = b.toString(16);//from www.ja va 2s.c o m if (str.length() == 16) str = str.substring(12); while (str.length() < 4) str = "0" + str; return str; }
From source file:com.bigdata.dastor.utils.FBUtilities.java
/** * Given two bit arrays represented as BigIntegers, containing the given * number of significant bits, calculate a midpoint. * * @param left The left point./* w w w .j a v a 2 s. com*/ * @param right The right point. * @param sigbits The number of bits in the points that are significant. * @return A midpoint that will compare bitwise halfway between the params, and * a boolean representing whether a non-zero lsbit remainder was generated. */ public static Pair<BigInteger, Boolean> midpoint(BigInteger left, BigInteger right, int sigbits) { BigInteger midpoint; boolean remainder; if (left.compareTo(right) < 0) { BigInteger sum = left.add(right); remainder = sum.testBit(0); midpoint = sum.shiftRight(1); } else { BigInteger max = TWO.pow(sigbits); // wrapping case BigInteger distance = max.add(right).subtract(left); remainder = distance.testBit(0); midpoint = distance.shiftRight(1).add(left).mod(max); } return new Pair(midpoint, remainder); }
From source file:org.apache.cassandra.utils.FBUtilities.java
/** * Given two bit arrays represented as BigIntegers, containing the given * number of significant bits, calculate a midpoint. * * @param left The left point.//from w w w .ja va 2s.com * @param right The right point. * @param sigbits The number of bits in the points that are significant. * @return A midpoint that will compare bitwise halfway between the params, and * a boolean representing whether a non-zero lsbit remainder was generated. */ public static Pair<BigInteger, Boolean> midpoint(BigInteger left, BigInteger right, int sigbits) { BigInteger midpoint; boolean remainder; if (left.compareTo(right) < 0) { BigInteger sum = left.add(right); remainder = sum.testBit(0); midpoint = sum.shiftRight(1); } else { BigInteger max = TWO.pow(sigbits); // wrapping case BigInteger distance = max.add(right).subtract(left); remainder = distance.testBit(0); midpoint = distance.shiftRight(1).add(left).mod(max); } return new Pair<BigInteger, Boolean>(midpoint, remainder); }
From source file:org.multibit.utils.CSMiscUtils.java
@Deprecated public static BigInteger calcTotalRawCharge(CSAsset asset, BigInteger numRawUnits) { BigInteger interestCharge = calcRawPercentageCharge(asset, numRawUnits); BigInteger flatCharge = calcRawFlatCharge(asset); System.out.println(">>>>> raw flat charge = " + flatCharge); return interestCharge.add(flatCharge); }
From source file:org.alfresco.mobile.android.api.network.NetworkHttpInvoker.java
private static Response invoke(UrlBuilder url, String method, String contentType, Map<String, List<String>> httpHeaders, Output writer, boolean forceOutput, BigInteger offset, BigInteger length, Map<String, String> params) { try {//from ww w. java 2 s. c om // Log.d("URL", url.toString()); // connect HttpURLConnection conn = (HttpURLConnection) (new URL(url.toString())).openConnection(); conn.setRequestMethod(method); conn.setDoInput(true); conn.setDoOutput(writer != null || forceOutput); conn.setAllowUserInteraction(false); conn.setUseCaches(false); conn.setRequestProperty("User-Agent", ClientVersion.OPENCMIS_CLIENT); // set content type if (contentType != null) { conn.setRequestProperty("Content-Type", contentType); } // set other headers if (httpHeaders != null) { for (Map.Entry<String, List<String>> header : httpHeaders.entrySet()) { if (header.getValue() != null) { for (String value : header.getValue()) { conn.addRequestProperty(header.getKey(), value); } } } } // range BigInteger tmpOffset = offset; if ((tmpOffset != null) || (length != null)) { StringBuilder sb = new StringBuilder("bytes="); if ((tmpOffset == null) || (tmpOffset.signum() == -1)) { tmpOffset = BigInteger.ZERO; } sb.append(tmpOffset.toString()); sb.append("-"); if ((length != null) && (length.signum() == 1)) { sb.append(tmpOffset.add(length.subtract(BigInteger.ONE)).toString()); } conn.setRequestProperty("Range", sb.toString()); } conn.setRequestProperty("Accept-Encoding", "gzip,deflate"); // add url form parameters if (params != null) { DataOutputStream ostream = null; OutputStream os = null; try { os = conn.getOutputStream(); ostream = new DataOutputStream(os); Set<String> parameters = params.keySet(); StringBuffer buf = new StringBuffer(); int paramCount = 0; for (String it : parameters) { String parameterName = it; String parameterValue = (String) params.get(parameterName); if (parameterValue != null) { parameterValue = URLEncoder.encode(parameterValue, "UTF-8"); if (paramCount > 0) { buf.append("&"); } buf.append(parameterName); buf.append("="); buf.append(parameterValue); ++paramCount; } } ostream.writeBytes(buf.toString()); } finally { if (ostream != null) { ostream.flush(); ostream.close(); } IOUtils.closeStream(os); } } // send data if (writer != null) { // conn.setChunkedStreamingMode((64 * 1024) - 1); OutputStream connOut = null; connOut = conn.getOutputStream(); OutputStream out = new BufferedOutputStream(connOut, BUFFER_SIZE); writer.write(out); out.flush(); } // connect conn.connect(); // get stream, if present int respCode = conn.getResponseCode(); InputStream inputStream = null; if ((respCode == HttpStatus.SC_OK) || (respCode == HttpStatus.SC_CREATED) || (respCode == HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION) || (respCode == HttpStatus.SC_PARTIAL_CONTENT)) { inputStream = conn.getInputStream(); } // get the response return new Response(respCode, conn.getResponseMessage(), conn.getHeaderFields(), inputStream, conn.getErrorStream()); } catch (Exception e) { throw new CmisConnectionException("Cannot access " + url + ": " + e.getMessage(), e); } }
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; }// w w w . j a v a 2 s . co 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; }