java.lang.Integer.numberOfLeadingZeros()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(195)

本文整理了Java中java.lang.Integer.numberOfLeadingZeros()方法的一些代码示例,展示了Integer.numberOfLeadingZeros()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Integer.numberOfLeadingZeros()方法的具体详情如下:
包路径:java.lang.Integer
类名称:Integer
方法名:numberOfLeadingZeros

Integer.numberOfLeadingZeros介绍

[英]Determines the number of leading zeros in the specified integer prior to the #highestOneBit(int).
[中]确定指定整数中#highestOneBit(int)之前的前导零数。

代码示例

代码示例来源:origin: ReactiveX/RxJava

  1. /**
  2. * Find the next larger positive power of two value up from the given value. If value is a power of two then
  3. * this value will be returned.
  4. *
  5. * @param value from which next positive power of two will be found.
  6. * @return the next positive power of 2 or this value if it is a power of 2.
  7. */
  8. public static int roundToPowerOfTwo(final int value) {
  9. return 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
  10. }

代码示例来源:origin: netty/netty

  1. private static int log2(int val) {
  2. // compute the (0-based, with lsb = 0) position of highest set bit i.e, log2
  3. return INTEGER_SIZE_MINUS_ONE - Integer.numberOfLeadingZeros(val);
  4. }

代码示例来源:origin: netty/netty

  1. /**
  2. * Fast method of finding the next power of 2 greater than or equal to the supplied value.
  3. *
  4. * <p>If the value is {@code <= 0} then 1 will be returned.
  5. * This method is not suitable for {@link Integer#MIN_VALUE} or numbers greater than 2^30.
  6. *
  7. * @param value from which to search for next power of 2
  8. * @return The next power of 2 or the value itself if it is a power of 2
  9. */
  10. public static int findNextPositivePowerOfTwo(final int value) {
  11. assert value > Integer.MIN_VALUE && value < 0x40000000;
  12. return 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
  13. }

代码示例来源:origin: LMAX-Exchange/disruptor

  1. /**
  2. * Calculate the next power of 2, greater than or equal to x.<p>
  3. * From Hacker's Delight, Chapter 3, Harry S. Warren Jr.
  4. *
  5. * @param x Value to round up
  6. * @return The next power of 2 from x inclusive
  7. */
  8. public static int ceilingNextPowerOfTwo(final int x)
  9. {
  10. return 1 << (32 - Integer.numberOfLeadingZeros(x - 1));
  11. }

代码示例来源:origin: prestodb/presto

  1. private static int encodedLengthSize(int length)
  2. {
  3. if (length < 128) {
  4. return 1;
  5. }
  6. int numberOfBits = 32 - Integer.numberOfLeadingZeros(length);
  7. int numberOfBytes = (numberOfBits + 7) / 8;
  8. return numberOfBytes + 1;
  9. }

代码示例来源:origin: redisson/redisson

  1. /**
  2. * Find the next larger positive power of two value up from the given value. If value is a power of two then
  3. * this value will be returned.
  4. *
  5. * @param value from which next positive power of two will be found.
  6. * @return the next positive power of 2 or this value if it is a power of 2.
  7. */
  8. public static int roundToPowerOfTwo(final int value) {
  9. return 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
  10. }

代码示例来源:origin: apache/flink

  1. public static int getBitSize(long value) {
  2. if (value > Integer.MAX_VALUE) {
  3. return 64 - Integer.numberOfLeadingZeros((int) (value >> 32));
  4. } else {
  5. return 32 - Integer.numberOfLeadingZeros((int) value);
  6. }
  7. }

代码示例来源:origin: redisson/redisson

  1. int computeListIndex( int len ) {
  2. int powIndex = 32-Integer.numberOfLeadingZeros(len-1);
  3. return powIndex;
  4. }

代码示例来源:origin: redisson/redisson

  1. /**
  2. * Fast method of finding the next power of 2 greater than or equal to the supplied value.
  3. *
  4. * <p>If the value is {@code <= 0} then 1 will be returned.
  5. * This method is not suitable for {@link Integer#MIN_VALUE} or numbers greater than 2^30.
  6. *
  7. * @param value from which to search for next power of 2
  8. * @return The next power of 2 or the value itself if it is a power of 2
  9. */
  10. public static int findNextPositivePowerOfTwo(final int value) {
  11. assert value > Integer.MIN_VALUE && value < 0x40000000;
  12. return 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
  13. }

代码示例来源:origin: redisson/redisson

  1. private static int log2(int val) {
  2. // compute the (0-based, with lsb = 0) position of highest set bit i.e, log2
  3. return INTEGER_SIZE_MINUS_ONE - Integer.numberOfLeadingZeros(val);
  4. }

代码示例来源:origin: redisson/redisson

  1. int computeLen( int len ) {
  2. int powIndex = 32-Integer.numberOfLeadingZeros(len-1);
  3. return 1<<powIndex;
  4. }

代码示例来源:origin: netty/netty

  1. private static int validateAndCalculatePageShifts(int pageSize) {
  2. if (pageSize < MIN_PAGE_SIZE) {
  3. throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: " + MIN_PAGE_SIZE + ")");
  4. }
  5. if ((pageSize & pageSize - 1) != 0) {
  6. throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: power of 2)");
  7. }
  8. // Logarithm base 2. At this point we know that pageSize is a power of two.
  9. return Integer.SIZE - 1 - Integer.numberOfLeadingZeros(pageSize);
  10. }

代码示例来源:origin: netty/netty

  1. /**
  2. * Calculates compression level on the basis of block size.
  3. */
  4. private static int compressionLevel(int blockSize) {
  5. if (blockSize < MIN_BLOCK_SIZE || blockSize > MAX_BLOCK_SIZE) {
  6. throw new IllegalArgumentException(String.format(
  7. "blockSize: %d (expected: %d-%d)", blockSize, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE));
  8. }
  9. int compressionLevel = 32 - Integer.numberOfLeadingZeros(blockSize - 1); // ceil of log2
  10. compressionLevel = Math.max(0, compressionLevel - COMPRESSION_LEVEL_BASE);
  11. return compressionLevel;
  12. }

代码示例来源:origin: redisson/redisson

  1. private static int validateAndCalculatePageShifts(int pageSize) {
  2. if (pageSize < MIN_PAGE_SIZE) {
  3. throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: " + MIN_PAGE_SIZE + ")");
  4. }
  5. if ((pageSize & pageSize - 1) != 0) {
  6. throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: power of 2)");
  7. }
  8. // Logarithm base 2. At this point we know that pageSize is a power of two.
  9. return Integer.SIZE - 1 - Integer.numberOfLeadingZeros(pageSize);
  10. }

代码示例来源:origin: redisson/redisson

  1. /**
  2. * Calculates compression level on the basis of block size.
  3. */
  4. private static int compressionLevel(int blockSize) {
  5. if (blockSize < MIN_BLOCK_SIZE || blockSize > MAX_BLOCK_SIZE) {
  6. throw new IllegalArgumentException(String.format(
  7. "blockSize: %d (expected: %d-%d)", blockSize, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE));
  8. }
  9. int compressionLevel = 32 - Integer.numberOfLeadingZeros(blockSize - 1); // ceil of log2
  10. compressionLevel = Math.max(0, compressionLevel - COMPRESSION_LEVEL_BASE);
  11. return compressionLevel;
  12. }

代码示例来源:origin: google/guava

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

代码示例来源:origin: google/guava

  1. /**
  2. * Returns the smallest power of two greater than or equal to {@code x}. This is equivalent to
  3. * {@code checkedPow(2, log2(x, CEILING))}.
  4. *
  5. * @throws IllegalArgumentException if {@code x <= 0}
  6. * @throws ArithmeticException of the next-higher power of two is not representable as an {@code
  7. * int}, i.e. when {@code x > 2^30}
  8. * @since 20.0
  9. */
  10. @Beta
  11. public static int ceilingPowerOfTwo(int x) {
  12. checkPositive("x", x);
  13. if (x > MAX_SIGNED_POWER_OF_TWO) {
  14. throw new ArithmeticException("ceilingPowerOfTwo(" + x + ") not representable as an int");
  15. }
  16. return 1 << -Integer.numberOfLeadingZeros(x - 1);
  17. }

代码示例来源:origin: neo4j/neo4j

  1. public NodeLabelsCache( NumberArrayFactory cacheFactory, int highLabelId, int chunkSize )
  2. {
  3. this.cache = cacheFactory.newDynamicLongArray( chunkSize, 0 );
  4. this.spillOver = cacheFactory.newDynamicLongArray( chunkSize / 5, 0 ); // expect way less of these
  5. this.bitsPerLabel = max( Integer.SIZE - numberOfLeadingZeros( highLabelId ), 1 );
  6. this.worstCaseLongsNeeded = ((bitsPerLabel * (highLabelId + 1 /*length slot*/)) - 1) / Long.SIZE + 1;
  7. this.putClient = new Client( worstCaseLongsNeeded );
  8. }

代码示例来源:origin: libgdx/libgdx

  1. @Override
  2. public Mesh obtain (VertexAttributes vertexAttributes, int vertexCount, int indexCount) {
  3. for (int i = 0, n = freeMeshes.size; i < n; ++i) {
  4. final Mesh mesh = freeMeshes.get(i);
  5. if (mesh.getVertexAttributes().equals(vertexAttributes) && mesh.getMaxVertices() >= vertexCount
  6. && mesh.getMaxIndices() >= indexCount) {
  7. freeMeshes.removeIndex(i);
  8. usedMeshes.add(mesh);
  9. return mesh;
  10. }
  11. }
  12. vertexCount = 1 + (int)Short.MAX_VALUE;
  13. indexCount = Math.max(1 + (int)Short.MAX_VALUE, 1 << (32 - Integer.numberOfLeadingZeros(indexCount - 1)));
  14. Mesh result = new Mesh(false, vertexCount, indexCount, vertexAttributes);
  15. usedMeshes.add(result);
  16. return result;
  17. }

代码示例来源:origin: libgdx/libgdx

  1. @Override
  2. public Mesh obtain (VertexAttributes vertexAttributes, int vertexCount, int indexCount) {
  3. for (int i = 0, n = freeMeshes.size; i < n; ++i) {
  4. final Mesh mesh = freeMeshes.get(i);
  5. if (mesh.getVertexAttributes().equals(vertexAttributes) && mesh.getMaxVertices() >= vertexCount
  6. && mesh.getMaxIndices() >= indexCount) {
  7. freeMeshes.removeIndex(i);
  8. usedMeshes.add(mesh);
  9. return mesh;
  10. }
  11. }
  12. vertexCount = 1 + (int)Short.MAX_VALUE;
  13. indexCount = Math.max(1 + (int)Short.MAX_VALUE, 1 << (32 - Integer.numberOfLeadingZeros(indexCount - 1)));
  14. Mesh result = new Mesh(false, vertexCount, indexCount, vertexAttributes);
  15. usedMeshes.add(result);
  16. return result;
  17. }

相关文章