List of usage examples for java.math BigInteger and
public BigInteger and(BigInteger val)
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.and(bi); }
From source file:Main.java
public static void main(String[] args) { // assign values to bi1, bi2 BigInteger bi1 = new BigInteger("6"); // 110 BigInteger bi2 = new BigInteger("3"); // 011 // perform and operation on bi1 using bi2 BigInteger bi3 = bi1.and(bi2); System.out.println(bi3);//from w ww. ja v a 2 s. c o m }
From source file:Main.java
/** * Convert to base 128 (bigendian), using shifts. * @param val//from w ww . j av a 2s. com * @return */ public static ArrayList<Integer> base128(BigInteger val) { ArrayList<Integer> result = new ArrayList<Integer>(); int part = val.and(BN127).intValue(); val = val.shiftRight(7); result.add(0, new Integer(part)); while (!val.equals(BigInteger.ZERO)) { part = val.and(BN127).intValue(); val = val.shiftRight(7); part += 128; result.add(0, new Integer(part)); } ; return result; }
From source file:Main.java
static BigInteger mulmod(BigInteger a, BigInteger b, BigInteger p) { BigInteger r = BigInteger.ZERO; while (b.compareTo(BigInteger.ZERO) > 0) { if (!b.and(BigInteger.ONE).equals(BigInteger.ZERO)) { r = addmod(r, a, p);/*from w w w. ja v a 2 s . com*/ } b = b.shiftRight(1); a = addmod(a, a, p); } return r; }
From source file:biz.karms.sinkit.ejb.util.CIDRUtils.java
public static ImmutablePair<String, String> getStartEndAddresses(final String cidr) throws UnknownHostException { //TODO: This is silly. Refactor CIDRUtils so as to accept actual IPs as well as subnets. //TODO: Validate the thing before processing. Guava? final String fixedCIDR; if (!cidr.contains("/")) { //IPv6? Hmmm... if (cidr.contains(":")) { fixedCIDR = cidr + "/128"; } else {/*from w ww.ja v a2s . com*/ fixedCIDR = cidr + "/32"; } } else { fixedCIDR = cidr; } final int index = fixedCIDR.indexOf("/"); final InetAddress inetAddress = InetAddress.getByName(fixedCIDR.substring(0, index)); final int prefixLength = Integer.parseInt(fixedCIDR.substring(index + 1)); final ByteBuffer maskBuffer; if (inetAddress.getAddress().length == 4) { maskBuffer = ByteBuffer.allocate(4).putInt(-1); } else { maskBuffer = ByteBuffer.allocate(16).putLong(-1L).putLong(-1L); } final BigInteger mask = (new BigInteger(1, maskBuffer.array())).not().shiftRight(prefixLength); final ByteBuffer buffer = ByteBuffer.wrap(inetAddress.getAddress()); final BigInteger ipVal = new BigInteger(1, buffer.array()); final BigInteger startIp = ipVal.and(mask); final BigInteger endIp = startIp.add(mask.not()); return new ImmutablePair<>(String.format("%040d", startIp), String.format("%040d", endIp)); }
From source file:org.voltdb.hadoop.Digester.java
/** * Generate a {@code UUID} representation of the MD5 digest for the given content. This is possible because MD5 * digests and UUIDs are both 128 bit long * * @param aContent//w w w . ja v a 2 s . c o m * a content {@code String} * @return a {@code UUID} representation of the computed MD5 digest * @throws DigesterException * upon failed cryptographic operation */ public final static UUID digestMD5asUUID(String aContent) throws DigestException { if (aContent == null) return null; BigInteger bi = digestMD5asBigInteger(aContent); return new UUID(bi.shiftRight(64).longValue(), bi.and(LSB_MASK).longValue()); }
From source file:com.wms.utils.DataUtil.java
public static String numberToIpv4(BigInteger ipNumber) { String ipString = ""; BigInteger a = new BigInteger("FF", 16); for (int i = 0; i < 4; i++) { ipString = ipNumber.and(a).toString() + "." + ipString; ipNumber = ipNumber.shiftRight(8); }// ww w . j av a2 s .co m return ipString.substring(0, ipString.length() - 1); }
From source file:com.wms.utils.DataUtil.java
public static String numberToIPv6(BigInteger ipNumber) { String ipString = ""; BigInteger a = new BigInteger("FFFF", 16); for (int i = 0; i < 8; i++) { ipString = ipNumber.and(a).toString(16) + ":" + ipString; ipNumber = ipNumber.shiftRight(16); }//from w w w. j av a 2 s .c o m return ipString.substring(0, ipString.length() - 1); }
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 w w . j a v a 2s . co 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; } }
From source file:net.spfbl.whois.SubnetIPv6.java
public static String getPreviousIPv6(String ip) { if (ip == null) { return null; } else if (SubnetIPv6.isValidIPv6(ip)) { BigInteger address = new BigInteger(1, address(ip)); if (address.equals(ADDRESS_MIN)) { return null; } else {/* w w w .ja v a 2 s .c om*/ address = address.subtract(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; } }