private void updateTokenizerValidation(Object tokenizer, int index) { if(tokenizer != null) { this.tokenizerValidator = this.tokenizerValidator.flipBit(index); } else { this.tokenizerValidator = this.tokenizerValidator.clearBit(index); } }
private static BigInteger rotateLeft(BigInteger bi) { BigInteger ret = bi.shiftLeft(1); if (ret.testBit(32)) { ret = ret.clearBit(32).setBit(0); } return ret; }
@Override BigInteger apply(BigInteger left, BigInteger right) { return left.clearBit(right.intValue()); } }
/** @return 0 to max-1 or -1 for us */ public int getRange(T key) { Integer rv; synchronized (_distanceCache) { rv = _distanceCache.get(key); if (rv == null) { // easy way when _bValue == 1 //rv = Integer.valueOf(_bigUs.xor(new BigInteger(1, key.getData())).bitLength() - 1); BigInteger xor = _bigUs.xor(new BigInteger(1, key.getData())); int range = xor.bitLength() - 1; if (_bValue > 1) { int toShift = range + 1 - _bValue; int highbit = range; range <<= _bValue - 1; if (toShift >= 0) { int extra = xor.clearBit(highbit).shiftRight(toShift).intValue(); range += extra; //Log log = I2PAppContext.getGlobalContext().logManager().getLog(KBucketSet.class); //if (log.shouldLog(Log.DEBUG)) // log.debug("highbit " + highbit + " toshift " + toShift + " extra " + extra + " new " + range); } } rv = Integer.valueOf(range); _distanceCache.put(key, rv); } } return rv.intValue(); }
/** * Default class constructor. Constructs the object with a value of zero(0). * */ public SnmpCounter64() { m_value = BigInteger.valueOf(0L); m_value.clearBit(65); // ensures that the 64-bits are treated as // unsigned! }
/** * Constructs a SnmpCounter64 object with the specified value. * * @param value * The new 64-bit value. * */ public SnmpCounter64(long value) { m_value = BigInteger.valueOf(value & Long.MAX_VALUE); m_value.clearBit(65); // ensures that the 64-bits are treated as // unsigned! }
/** * Constructs a SnmpCounter64 object with the specified value. * * @param value * The new 64-bit value. * */ public SnmpCounter64(BigInteger value) { m_value = new BigInteger(value.toByteArray()); m_value.clearBit(65); // ensures that the 64-bits are treated as // unsigned! }
/** * Used to set the 64-bit unsigned quantity. If the value exceeds 64-bit * then the upper 64-bits will be silently truncated from the value. * * @param value * The new value for the object */ public void setValue(BigInteger value) { m_value = new BigInteger(value.toByteArray()); m_value.clearBit(65); // ensure 64-bit unsigned comparisions! }
/** * Class copy constructor. Constructs a new object with the same value as * the passed object. * * @param second * The object to copy the value from. * */ public SnmpCounter64(SnmpCounter64 second) { m_value = new BigInteger(second.m_value.toByteArray()); m_value.clearBit(65); // ensures that the 64-bits are treated as // unsigned! }
class Test { public static void main(String[] args) throws Exception { Long value = 12L; BigInteger b = new BigInteger(String.valueOf(value)); System.out.println(b.toString(2) + " " + value); b = b.setBit(1); b = b.clearBit(2); value = Long.valueOf(b.toString()); System.out.println(b.toString(2) + " " + value); } }
@Override protected boolean isPowerOfTwo(Object value) { BigInteger bintValue = (BigInteger) value; int bitIndex = bintValue.getLowestSetBit(); if (bitIndex < 0) { return false; } return bintValue.clearBit(bitIndex).equals(BigInteger.ZERO); } };
private void updateTokenizerValidation(Object tokenizer, int index) { if(tokenizer != null) { this.tokenizerValidator = this.tokenizerValidator.flipBit(index); } else { this.tokenizerValidator = this.tokenizerValidator.clearBit(index); } }
private BigInteger getMaskedAddress(boolean one) { BigInteger numAddress = netAddress; int numBits; if (isV4) { numBits = 32 - networkMask; } else { numBits = 128 - networkMask; } for (int i = 0; i < numBits; i++) { if (one) numAddress = numAddress.setBit(i); else numAddress = numAddress.clearBit(i); } return numAddress; }
public static Number bitAt_put_(BigInteger receiver, int bitIndex, int bitValue) { if (bitValue == 1) { return IntegerExtensions.objectFromBigInteger(receiver .setBit(bitIndex)); } else if (bitValue == 0) { return IntegerExtensions.objectFromBigInteger(receiver .clearBit(bitIndex)); } else { throw new RuntimeException(); } }
private synchronized BigInteger[] generateParameters() { // N A large safe prime (N = 2q+1, where q is prime) // g A generator modulo N BigInteger q, p, g; byte[] qBytes = new byte[l / 8]; do { do { nextRandomBytes(qBytes); q = new BigInteger(1, qBytes); q = q.setBit(0).setBit(l-2).clearBit(l-1); } while (!Prime.isProbablePrime(q)); p = q.multiply(TWO).add(ONE); } while (p.bitLength() != l || !Prime.isProbablePrime(p)); // compute g. from FIPS-186, Appendix 4: e == 2 BigInteger p_minus_1 = p.subtract(ONE); g = TWO; // Set h = any integer, where 1 < h < p - 1 and // h differs from any value previously tried for (BigInteger h = TWO; h.compareTo(p_minus_1) < 0; h = h.add(ONE)) { // Set g = h**2 mod p g = h.modPow(TWO, p); // If g = 1, go to step 3 if (!g.equals(ONE)) { break; } } return new BigInteger[] { q, p, g }; }
private BigInteger[] computeRS(final byte[] digestBytes) { final BigInteger p = ((DSAPrivateKey) privateKey).getParams().getP(); final BigInteger q = ((DSAPrivateKey) privateKey).getParams().getQ(); final BigInteger g = ((DSAPrivateKey) privateKey).getParams().getG(); final BigInteger x = ((DSAPrivateKey) privateKey).getX(); final BigInteger m = new BigInteger(1, digestBytes); BigInteger k, r, s; final byte[] kb = new byte[20]; // we'll use 159 bits only while (true) { this.nextRandomBytes(kb); k = new BigInteger(1, kb); k.clearBit(159); r = g.modPow(k, p).mod(q); if (r.equals(BigInteger.ZERO)) { continue; } s = m.add(x.multiply(r)).multiply(k.modInverse(q)).mod(q); if (s.equals(BigInteger.ZERO)) { continue; } break; } return new BigInteger[] {r, s}; }