Tabnine Logo
Long.numberOfLeadingZeros
Code IndexAdd Tabnine to your IDE (free)

How to use
numberOfLeadingZeros
method
in
java.lang.Long

Best Java code snippets using java.lang.Long.numberOfLeadingZeros (Showing top 20 results out of 2,934)

origin: prestodb/presto

private static int getWriteByteCount(long value)
{
  // if the value is negative flip the bits, so we can always count leading zero bytes
  if (value < 0) {
    value = ~value;
  }
  // count number of leading zero bytes
  return (Long.SIZE - Long.numberOfLeadingZeros(value)) / BITS_IN_BYTE + 1;
}
origin: ben-manes/caffeine

 static long ceilingPowerOfTwo(long x) {
  // From Hacker's Delight, Chapter 3, Harry S. Warren Jr.
  return 1L << -Long.numberOfLeadingZeros(x - 1);
 }
}
origin: pxb1988/dex2jar

public static int lengthOfSint(long value) {
  int nbBits = 65 - Long.numberOfLeadingZeros(value ^ (value >> 63));
  return (nbBits + 0x07) >> 3;
}
origin: jankotek/mapdb

public static int packLong(byte[] buf, int pos, long value){
  int pos2 = pos;
  int shift = 63-Long.numberOfLeadingZeros(value);
  shift -= shift%7; // round down to nearest multiple of 7
  while(shift!=0){
    buf[pos++] = (byte) ((value>>>shift) & 0x7F);
    shift-=7;
  }
  buf[pos++] = (byte) ((value & 0x7F) | 0x80);
  return pos - pos2;
}
origin: google/guava

/**
 * Returns the product of {@code a} and {@code b} unless it would overflow or underflow in which
 * case {@code Long.MAX_VALUE} or {@code Long.MIN_VALUE} is returned, respectively.
 *
 * @since 20.0
 */
@Beta
public static long saturatedMultiply(long a, long b) {
 // see checkedMultiply for explanation
 int leadingZeros =
   Long.numberOfLeadingZeros(a)
     + Long.numberOfLeadingZeros(~a)
     + Long.numberOfLeadingZeros(b)
     + Long.numberOfLeadingZeros(~b);
 if (leadingZeros > Long.SIZE + 1) {
  return a * b;
 }
 // the return value if we will overflow (which we calculate by overflowing a long :) )
 long limit = Long.MAX_VALUE + ((a ^ b) >>> (Long.SIZE - 1));
 if (leadingZeros < Long.SIZE | (a < 0 & b == Long.MIN_VALUE)) {
  // overflow
  return limit;
 }
 long result = a * b;
 if (a == 0 || result / a == b) {
  return result;
 }
 return limit;
}
origin: google/guava

/** Returns (a * 2^32) mod m. a may be any unsigned long. */
private long times2ToThe32Mod(long a, long m) {
 int remainingPowersOf2 = 32;
 do {
  int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a));
  // shift is either the number of powers of 2 left to multiply a by, or the biggest shift
  // possible while keeping a in an unsigned long.
  a = UnsignedLongs.remainder(a << shift, m);
  remainingPowersOf2 -= shift;
 } while (remainingPowersOf2 > 0);
 return a;
}
origin: google/guava

/**
 * Returns the largest power of two less than or equal to {@code x}. This is equivalent to {@code
 * checkedPow(2, log2(x, FLOOR))}.
 *
 * @throws IllegalArgumentException if {@code x <= 0}
 * @since 20.0
 */
@Beta
public static long floorPowerOfTwo(long x) {
 checkPositive("x", x);
 // Long.highestOneBit was buggy on GWT.  We've fixed it, but I'm not certain when the fix will
 // be released.
 return 1L << ((Long.SIZE - 1) - Long.numberOfLeadingZeros(x));
}
origin: google/guava

/**
 * Returns the smallest power of two greater than or equal to {@code x}. This is equivalent to
 * {@code checkedPow(2, log2(x, CEILING))}.
 *
 * @throws IllegalArgumentException if {@code x <= 0}
 * @throws ArithmeticException of the next-higher power of two is not representable as a {@code
 *     long}, i.e. when {@code x > 2^62}
 * @since 20.0
 */
@Beta
public static long ceilingPowerOfTwo(long x) {
 checkPositive("x", x);
 if (x > MAX_SIGNED_POWER_OF_TWO) {
  throw new ArithmeticException("ceilingPowerOfTwo(" + x + ") is not representable as a long");
 }
 return 1L << -Long.numberOfLeadingZeros(x - 1);
}
origin: google/guava

Long.numberOfLeadingZeros(a)
  + Long.numberOfLeadingZeros(~a)
  + Long.numberOfLeadingZeros(b)
  + Long.numberOfLeadingZeros(~b);
origin: prestodb/presto

/** Returns (a * 2^32) mod m. a may be any unsigned long. */
private long times2ToThe32Mod(long a, long m) {
 int remainingPowersOf2 = 32;
 do {
  int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a));
  // shift is either the number of powers of 2 left to multiply a by, or the biggest shift
  // possible while keeping a in an unsigned long.
  a = UnsignedLongs.remainder(a << shift, m);
  remainingPowersOf2 -= shift;
 } while (remainingPowersOf2 > 0);
 return a;
}
origin: neo4j/neo4j

/**
 * Returns base 2 logarithm of the closest power of 2 that is less or equal to the {@code value}.
 *
 * @param value a positive long value
 */
public static int log2floor( long value )
{
  return (Long.SIZE - 1) - Long.numberOfLeadingZeros( requirePositive( value ) );
}
origin: google/j2objc

/** Returns (a * 2^32) mod m. a may be any unsigned long. */
private long times2ToThe32Mod(long a, long m) {
 int remainingPowersOf2 = 32;
 do {
  int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a));
  // shift is either the number of powers of 2 left to multiply a by, or the biggest shift
  // possible while keeping a in an unsigned long.
  a = UnsignedLongs.remainder(a << shift, m);
  remainingPowersOf2 -= shift;
 } while (remainingPowersOf2 > 0);
 return a;
}
origin: thinkaurelius/titan

/**
 * Returns an integer X such that 2^X=value. Throws an exception
 * if value is not a power of 2.
 *
 * @param value
 * @return
 */
public static int getPowerOf2(long value) {
  Preconditions.checkArgument(isPowerOf2(value));
  return Long.SIZE-(Long.numberOfLeadingZeros(value)+1);
}
origin: google/j2objc

/**
 * Returns the largest power of two less than or equal to {@code x}. This is equivalent to {@code
 * checkedPow(2, log2(x, FLOOR))}.
 *
 * @throws IllegalArgumentException if {@code x <= 0}
 * @since 20.0
 */
@Beta
public static long floorPowerOfTwo(long x) {
 checkPositive("x", x);
 // Long.highestOneBit was buggy on GWT.  We've fixed it, but I'm not certain when the fix will
 // be released.
 return 1L << ((Long.SIZE - 1) - Long.numberOfLeadingZeros(x));
}
origin: jankotek/mapdb

public void packLong(long value) {
  ensureAvail(10); //ensure worst case bytes
  int shift = 63-Long.numberOfLeadingZeros(value);
  shift -= shift%7; // round down to nearest multiple of 7
  while(shift!=0){
    buf[pos++] = (byte) ((value>>>shift) & 0x7F);
    shift-=7;
  }
  buf[pos++] = (byte) ((value & 0x7F) | 0x80);
}
origin: google/guava

@GwtIncompatible // TODO
static int log10Floor(long x) {
 /*
  * Based on Hacker's Delight Fig. 11-5, the two-table-lookup, branch-free implementation.
  *
  * The key idea is that based on the number of leading zeros (equivalently, floor(log2(x))), we
  * can narrow the possible floor(log10(x)) values to two. For example, if floor(log2(x)) is 6,
  * then 64 <= x < 128, so floor(log10(x)) is either 1 or 2.
  */
 int y = maxLog10ForLeadingZeros[Long.numberOfLeadingZeros(x)];
 /*
  * y is the higher of the two possible values of floor(log10(x)). If x < 10^y, then we want the
  * lower of the two possible values, or y - 1, otherwise, we want y.
  */
 return y - lessThanBranchFree(x, powersOf10[y]);
}
origin: google/guava

 return (Long.SIZE - 1) - Long.numberOfLeadingZeros(x);
 return Long.SIZE - Long.numberOfLeadingZeros(x - 1);
case HALF_EVEN:
 int leadingZeros = Long.numberOfLeadingZeros(x);
 long cmp = MAX_POWER_OF_SQRT2_UNSIGNED >>> leadingZeros;
origin: prestodb/presto

/**
 * Returns the smallest power of two greater than or equal to {@code x}. This is equivalent to
 * {@code checkedPow(2, log2(x, CEILING))}.
 *
 * @throws IllegalArgumentException if {@code x <= 0}
 * @throws ArithmeticException of the next-higher power of two is not representable as a {@code
 *     long}, i.e. when {@code x > 2^62}
 * @since 20.0
 */
@Beta
public static long ceilingPowerOfTwo(long x) {
 checkPositive("x", x);
 if (x > MAX_SIGNED_POWER_OF_TWO) {
  throw new ArithmeticException("ceilingPowerOfTwo(" + x + ") is not representable as a long");
 }
 return 1L << -Long.numberOfLeadingZeros(x - 1);
}
origin: prestodb/presto

/**
 * Returns the largest power of two less than or equal to {@code x}. This is equivalent to {@code
 * checkedPow(2, log2(x, FLOOR))}.
 *
 * @throws IllegalArgumentException if {@code x <= 0}
 * @since 20.0
 */
@Beta
public static long floorPowerOfTwo(long x) {
 checkPositive("x", x);
 // Long.highestOneBit was buggy on GWT.  We've fixed it, but I'm not certain when the fix will
 // be released.
 return 1L << ((Long.SIZE - 1) - Long.numberOfLeadingZeros(x));
}
origin: prestodb/presto

@GwtIncompatible // TODO
static int log10Floor(long x) {
 /*
  * Based on Hacker's Delight Fig. 11-5, the two-table-lookup, branch-free implementation.
  *
  * The key idea is that based on the number of leading zeros (equivalently, floor(log2(x))), we
  * can narrow the possible floor(log10(x)) values to two. For example, if floor(log2(x)) is 6,
  * then 64 <= x < 128, so floor(log10(x)) is either 1 or 2.
  */
 int y = maxLog10ForLeadingZeros[Long.numberOfLeadingZeros(x)];
 /*
  * y is the higher of the two possible values of floor(log10(x)). If x < 10^y, then we want the
  * lower of the two possible values, or y - 1, otherwise, we want y.
  */
 return y - lessThanBranchFree(x, powersOf10[y]);
}
java.langLongnumberOfLeadingZeros

Javadoc

Determines the number of leading zeros in the specified long value prior to the #highestOneBit(long).

Popular methods of Long

  • parseLong
    Parses the string argument as a signed long in the radix specified by the second argument. The chara
  • toString
    Returns a string representation of the first argument in the radix specified by the second argument.
  • valueOf
    Returns a Long object holding the value extracted from the specified String when parsed with the rad
  • longValue
    Returns the value of this Long as a long value.
  • <init>
    Constructs a newly allocated Long object that represents the long value indicated by the String para
  • intValue
    Returns the value of this Long as an int.
  • equals
    Compares this object to the specified object. The result is true if and only if the argument is not
  • hashCode
  • toHexString
    Returns a string representation of the longargument as an unsigned integer in base 16.The unsigned l
  • compareTo
    Compares this Long object to another object. If the object is a Long, this function behaves likecomp
  • compare
    Compares two long values numerically. The value returned is identical to what would be returned by:
  • doubleValue
    Returns the value of this Long as a double.
  • compare,
  • doubleValue,
  • decode,
  • numberOfTrailingZeros,
  • bitCount,
  • signum,
  • reverseBytes,
  • toBinaryString,
  • shortValue

Popular in Java

  • Finding current android device location
  • putExtra (Intent)
  • scheduleAtFixedRate (Timer)
  • getResourceAsStream (ClassLoader)
  • Component (java.awt)
    A component is an object having a graphical representation that can be displayed on the screen and t
  • FileReader (java.io)
    A specialized Reader that reads from a file in the file system. All read requests made by calling me
  • Dictionary (java.util)
    Note: Do not use this class since it is obsolete. Please use the Map interface for new implementatio
  • ThreadPoolExecutor (java.util.concurrent)
    An ExecutorService that executes each submitted task using one of possibly several pooled threads, n
  • Location (org.springframework.beans.factory.parsing)
    Class that models an arbitrary location in a Resource.Typically used to track the location of proble
  • SAXParseException (org.xml.sax)
    Encapsulate an XML parse error or warning.> This module, both source code and documentation, is in t
  • From CI to AI: The AI layer in your organization
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now