本文整理了Java中java.lang.Integer.numberOfLeadingZeros()
方法的一些代码示例,展示了Integer.numberOfLeadingZeros()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Integer.numberOfLeadingZeros()
方法的具体详情如下:
包路径:java.lang.Integer
类名称:Integer
方法名:numberOfLeadingZeros
[英]Determines the number of leading zeros in the specified integer prior to the #highestOneBit(int).
[中]确定指定整数中#highestOneBit(int)之前的前导零数。
代码示例来源:origin: ReactiveX/RxJava
/**
* Find the next larger positive power of two value up from the given value. If value is a power of two then
* this value will be returned.
*
* @param value from which next positive power of two will be found.
* @return the next positive power of 2 or this value if it is a power of 2.
*/
public static int roundToPowerOfTwo(final int value) {
return 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
}
代码示例来源:origin: netty/netty
private static int log2(int val) {
// compute the (0-based, with lsb = 0) position of highest set bit i.e, log2
return INTEGER_SIZE_MINUS_ONE - Integer.numberOfLeadingZeros(val);
}
代码示例来源:origin: netty/netty
/**
* Fast method of finding the next power of 2 greater than or equal to the supplied value.
*
* <p>If the value is {@code <= 0} then 1 will be returned.
* This method is not suitable for {@link Integer#MIN_VALUE} or numbers greater than 2^30.
*
* @param value from which to search for next power of 2
* @return The next power of 2 or the value itself if it is a power of 2
*/
public static int findNextPositivePowerOfTwo(final int value) {
assert value > Integer.MIN_VALUE && value < 0x40000000;
return 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
}
代码示例来源:origin: LMAX-Exchange/disruptor
/**
* Calculate the next power of 2, greater than or equal to x.<p>
* From Hacker's Delight, Chapter 3, Harry S. Warren Jr.
*
* @param x Value to round up
* @return The next power of 2 from x inclusive
*/
public static int ceilingNextPowerOfTwo(final int x)
{
return 1 << (32 - Integer.numberOfLeadingZeros(x - 1));
}
代码示例来源:origin: prestodb/presto
private static int encodedLengthSize(int length)
{
if (length < 128) {
return 1;
}
int numberOfBits = 32 - Integer.numberOfLeadingZeros(length);
int numberOfBytes = (numberOfBits + 7) / 8;
return numberOfBytes + 1;
}
代码示例来源:origin: redisson/redisson
/**
* Find the next larger positive power of two value up from the given value. If value is a power of two then
* this value will be returned.
*
* @param value from which next positive power of two will be found.
* @return the next positive power of 2 or this value if it is a power of 2.
*/
public static int roundToPowerOfTwo(final int value) {
return 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
}
代码示例来源:origin: apache/flink
public static int getBitSize(long value) {
if (value > Integer.MAX_VALUE) {
return 64 - Integer.numberOfLeadingZeros((int) (value >> 32));
} else {
return 32 - Integer.numberOfLeadingZeros((int) value);
}
}
代码示例来源:origin: redisson/redisson
int computeListIndex( int len ) {
int powIndex = 32-Integer.numberOfLeadingZeros(len-1);
return powIndex;
}
代码示例来源:origin: redisson/redisson
/**
* Fast method of finding the next power of 2 greater than or equal to the supplied value.
*
* <p>If the value is {@code <= 0} then 1 will be returned.
* This method is not suitable for {@link Integer#MIN_VALUE} or numbers greater than 2^30.
*
* @param value from which to search for next power of 2
* @return The next power of 2 or the value itself if it is a power of 2
*/
public static int findNextPositivePowerOfTwo(final int value) {
assert value > Integer.MIN_VALUE && value < 0x40000000;
return 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
}
代码示例来源:origin: redisson/redisson
private static int log2(int val) {
// compute the (0-based, with lsb = 0) position of highest set bit i.e, log2
return INTEGER_SIZE_MINUS_ONE - Integer.numberOfLeadingZeros(val);
}
代码示例来源:origin: redisson/redisson
int computeLen( int len ) {
int powIndex = 32-Integer.numberOfLeadingZeros(len-1);
return 1<<powIndex;
}
代码示例来源:origin: netty/netty
private static int validateAndCalculatePageShifts(int pageSize) {
if (pageSize < MIN_PAGE_SIZE) {
throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: " + MIN_PAGE_SIZE + ")");
}
if ((pageSize & pageSize - 1) != 0) {
throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: power of 2)");
}
// Logarithm base 2. At this point we know that pageSize is a power of two.
return Integer.SIZE - 1 - Integer.numberOfLeadingZeros(pageSize);
}
代码示例来源:origin: netty/netty
/**
* Calculates compression level on the basis of block size.
*/
private static int compressionLevel(int blockSize) {
if (blockSize < MIN_BLOCK_SIZE || blockSize > MAX_BLOCK_SIZE) {
throw new IllegalArgumentException(String.format(
"blockSize: %d (expected: %d-%d)", blockSize, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE));
}
int compressionLevel = 32 - Integer.numberOfLeadingZeros(blockSize - 1); // ceil of log2
compressionLevel = Math.max(0, compressionLevel - COMPRESSION_LEVEL_BASE);
return compressionLevel;
}
代码示例来源:origin: redisson/redisson
private static int validateAndCalculatePageShifts(int pageSize) {
if (pageSize < MIN_PAGE_SIZE) {
throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: " + MIN_PAGE_SIZE + ")");
}
if ((pageSize & pageSize - 1) != 0) {
throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: power of 2)");
}
// Logarithm base 2. At this point we know that pageSize is a power of two.
return Integer.SIZE - 1 - Integer.numberOfLeadingZeros(pageSize);
}
代码示例来源:origin: redisson/redisson
/**
* Calculates compression level on the basis of block size.
*/
private static int compressionLevel(int blockSize) {
if (blockSize < MIN_BLOCK_SIZE || blockSize > MAX_BLOCK_SIZE) {
throw new IllegalArgumentException(String.format(
"blockSize: %d (expected: %d-%d)", blockSize, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE));
}
int compressionLevel = 32 - Integer.numberOfLeadingZeros(blockSize - 1); // ceil of log2
compressionLevel = Math.max(0, compressionLevel - COMPRESSION_LEVEL_BASE);
return compressionLevel;
}
代码示例来源:origin: google/guava
private static int log10Floor(int 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[Integer.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
/**
* 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 an {@code
* int}, i.e. when {@code x > 2^30}
* @since 20.0
*/
@Beta
public static int ceilingPowerOfTwo(int x) {
checkPositive("x", x);
if (x > MAX_SIGNED_POWER_OF_TWO) {
throw new ArithmeticException("ceilingPowerOfTwo(" + x + ") not representable as an int");
}
return 1 << -Integer.numberOfLeadingZeros(x - 1);
}
代码示例来源:origin: neo4j/neo4j
public NodeLabelsCache( NumberArrayFactory cacheFactory, int highLabelId, int chunkSize )
{
this.cache = cacheFactory.newDynamicLongArray( chunkSize, 0 );
this.spillOver = cacheFactory.newDynamicLongArray( chunkSize / 5, 0 ); // expect way less of these
this.bitsPerLabel = max( Integer.SIZE - numberOfLeadingZeros( highLabelId ), 1 );
this.worstCaseLongsNeeded = ((bitsPerLabel * (highLabelId + 1 /*length slot*/)) - 1) / Long.SIZE + 1;
this.putClient = new Client( worstCaseLongsNeeded );
}
代码示例来源:origin: libgdx/libgdx
@Override
public Mesh obtain (VertexAttributes vertexAttributes, int vertexCount, int indexCount) {
for (int i = 0, n = freeMeshes.size; i < n; ++i) {
final Mesh mesh = freeMeshes.get(i);
if (mesh.getVertexAttributes().equals(vertexAttributes) && mesh.getMaxVertices() >= vertexCount
&& mesh.getMaxIndices() >= indexCount) {
freeMeshes.removeIndex(i);
usedMeshes.add(mesh);
return mesh;
}
}
vertexCount = 1 + (int)Short.MAX_VALUE;
indexCount = Math.max(1 + (int)Short.MAX_VALUE, 1 << (32 - Integer.numberOfLeadingZeros(indexCount - 1)));
Mesh result = new Mesh(false, vertexCount, indexCount, vertexAttributes);
usedMeshes.add(result);
return result;
}
代码示例来源:origin: libgdx/libgdx
@Override
public Mesh obtain (VertexAttributes vertexAttributes, int vertexCount, int indexCount) {
for (int i = 0, n = freeMeshes.size; i < n; ++i) {
final Mesh mesh = freeMeshes.get(i);
if (mesh.getVertexAttributes().equals(vertexAttributes) && mesh.getMaxVertices() >= vertexCount
&& mesh.getMaxIndices() >= indexCount) {
freeMeshes.removeIndex(i);
usedMeshes.add(mesh);
return mesh;
}
}
vertexCount = 1 + (int)Short.MAX_VALUE;
indexCount = Math.max(1 + (int)Short.MAX_VALUE, 1 << (32 - Integer.numberOfLeadingZeros(indexCount - 1)));
Mesh result = new Mesh(false, vertexCount, indexCount, vertexAttributes);
usedMeshes.add(result);
return result;
}
内容来源于网络,如有侵权,请联系作者删除!