本文整理了Java中java.lang.Integer.highestOneBit()
方法的一些代码示例,展示了Integer.highestOneBit()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Integer.highestOneBit()
方法的具体详情如下:
包路径:java.lang.Integer
类名称:Integer
方法名:highestOneBit
[英]Determines the highest (leftmost) bit of the specified integer that is 1 and returns the bit mask value for that bit. This is also referred to as the Most Significant 1 Bit. Returns zero if the specified integer is zero.
[中]确定指定整数的最高(最左边)位为1,并返回该位的位掩码值。这也称为最高有效1位。如果指定的整数为零,则返回零。
代码示例来源:origin: netty/netty
/**
* Calculates the minimum number of bits required to encode a value. This can
* then in turn be used to calculate the number of septets or octets (as
* appropriate) to use to encode a length parameter.
*
* @param value The value to calculate the minimum number of bits required to encode
* @return The minimum number of bits required to encode the supplied value
*/
private static int bitsToEncode(int value) {
int highestOneBit = Integer.highestOneBit(value);
int bitLength = 0;
while ((highestOneBit >>= 1) != 0) {
bitLength++;
}
return bitLength;
}
代码示例来源:origin: google/guava
static int closedTableSize(int expectedEntries, double loadFactor) {
// Get the recommended table size.
// Round down to the nearest power of 2.
expectedEntries = Math.max(expectedEntries, 2);
int tableSize = Integer.highestOneBit(expectedEntries);
// Check to make sure that we will not exceed the maximum load factor.
if (expectedEntries > (int) (loadFactor * tableSize)) {
tableSize <<= 1;
return (tableSize > 0) ? tableSize : MAX_TABLE_SIZE;
}
return tableSize;
}
代码示例来源:origin: apache/flink
/**
* Decrements the given number down to the closest power of two. If the argument is a
* power of two, it remains unchanged.
*
* @param value The value to round down.
* @return The closest value that is a power of two and less or equal than the given value.
*/
public static int roundDownToPowerOf2(int value) {
return Integer.highestOneBit(value);
}
代码示例来源:origin: prestodb/presto
private static int log2Ceiling(int value)
{
return Integer.highestOneBit(value - 1) << 1;
}
代码示例来源:origin: google/guava
private static int expandedCapacity(int oldCapacity, int minCapacity) {
if (minCapacity < 0) {
throw new AssertionError("cannot store more than MAX_VALUE elements");
}
// careful of overflow!
int newCapacity = oldCapacity + (oldCapacity >> 1) + 1;
if (newCapacity < minCapacity) {
newCapacity = Integer.highestOneBit(minCapacity - 1) << 1;
}
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE; // guaranteed to be >= newCapacity
}
return newCapacity;
}
代码示例来源:origin: google/guava
private static int expandedCapacity(int oldCapacity, int minCapacity) {
if (minCapacity < 0) {
throw new AssertionError("cannot store more than MAX_VALUE elements");
}
// careful of overflow!
int newCapacity = oldCapacity + (oldCapacity >> 1) + 1;
if (newCapacity < minCapacity) {
newCapacity = Integer.highestOneBit(minCapacity - 1) << 1;
}
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE; // guaranteed to be >= newCapacity
}
return newCapacity;
}
代码示例来源:origin: google/guava
private static int expandedCapacity(int oldCapacity, int minCapacity) {
if (minCapacity < 0) {
throw new AssertionError("cannot store more than MAX_VALUE elements");
}
// careful of overflow!
int newCapacity = oldCapacity + (oldCapacity >> 1) + 1;
if (newCapacity < minCapacity) {
newCapacity = Integer.highestOneBit(minCapacity - 1) << 1;
}
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE; // guaranteed to be >= newCapacity
}
return newCapacity;
}
代码示例来源:origin: google/guava
static int expandedCapacity(int oldCapacity, int minCapacity) {
if (minCapacity < 0) {
throw new AssertionError("cannot store more than MAX_VALUE elements");
}
// careful of overflow!
int newCapacity = oldCapacity + (oldCapacity >> 1) + 1;
if (newCapacity < minCapacity) {
newCapacity = Integer.highestOneBit(minCapacity - 1) << 1;
}
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
// guaranteed to be >= newCapacity
}
return newCapacity;
}
代码示例来源:origin: bumptech/glide
@Override
public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
int requestedHeight) {
int minIntegerFactor = Math.min(sourceHeight / requestedHeight, sourceWidth / requestedWidth);
return minIntegerFactor == 0 ? 1f : 1f / Integer.highestOneBit(minIntegerFactor);
}
代码示例来源:origin: google/guava
/**
* Returns an array size suitable for the backing array of a hash table that uses open addressing
* with linear probing in its implementation. The returned size is the smallest power of two that
* can hold setSize elements with the desired load factor.
*/
@VisibleForTesting
static int chooseTableSize(int setSize) {
if (setSize == 1) {
return 2;
}
// Correct the size for open addressing to match desired load factor.
// Round up to the next highest power of 2.
int tableSize = Integer.highestOneBit(setSize - 1) << 1;
while (tableSize * DESIRED_LOAD_FACTOR < setSize) {
tableSize <<= 1;
}
return tableSize;
}
代码示例来源:origin: prestodb/presto
static int closedTableSize(int expectedEntries, double loadFactor) {
// Get the recommended table size.
// Round down to the nearest power of 2.
expectedEntries = Math.max(expectedEntries, 2);
int tableSize = Integer.highestOneBit(expectedEntries);
// Check to make sure that we will not exceed the maximum load factor.
if (expectedEntries > (int) (loadFactor * tableSize)) {
tableSize <<= 1;
return (tableSize > 0) ? tableSize : MAX_TABLE_SIZE;
}
return tableSize;
}
代码示例来源:origin: bumptech/glide
@Override
public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
int requestedHeight) {
int maxIntegerFactor = (int) Math.ceil(Math.max(sourceHeight / (float) requestedHeight,
sourceWidth / (float) requestedWidth));
int lesserOrEqualSampleSize = Math.max(1, Integer.highestOneBit(maxIntegerFactor));
int greaterOrEqualSampleSize =
lesserOrEqualSampleSize << (lesserOrEqualSampleSize < maxIntegerFactor ? 1 : 0);
return 1f / greaterOrEqualSampleSize;
}
代码示例来源: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 int floorPowerOfTwo(int x) {
checkPositive("x", x);
return Integer.highestOneBit(x);
}
代码示例来源:origin: google/guava
/**
* Returns an array size suitable for the backing array of a hash table that uses open addressing
* with linear probing in its implementation. The returned size is the smallest power of two that
* can hold setSize elements with the desired load factor. Always returns at least setSize + 2.
*/
@VisibleForTesting
static int chooseTableSize(int setSize) {
setSize = Math.max(setSize, 2);
// Correct the size for open addressing to match desired load factor.
if (setSize < CUTOFF) {
// Round up to the next highest power of 2.
int tableSize = Integer.highestOneBit(setSize - 1) << 1;
while (tableSize * DESIRED_LOAD_FACTOR < setSize) {
tableSize <<= 1;
}
return tableSize;
}
// The table can't be completely full or we'll get infinite reprobes
checkArgument(setSize < MAX_TABLE_SIZE, "collection too large");
return MAX_TABLE_SIZE;
}
代码示例来源:origin: redisson/redisson
public FSTIdentity2IdMap(int initialSize) {
if (initialSize < 2) {
initialSize = 2;
}
initialSize = adjustSize(initialSize * GROFAC);
mKeys = new Object[initialSize];
mValues = new int[initialSize];
mNumberOfElements = 0;
mask = (Integer.highestOneBit(initialSize) << 1) - 1;
klen = initialSize - 4;
}
代码示例来源:origin: apache/incubator-druid
public static int maxIntsInBufferForBytes(int numBytes)
{
int maxSizePer = (CompressedPools.BUFFER_SIZE - bufferPadding(numBytes)) / numBytes;
// round down to the nearest power of 2
return Integer.highestOneBit(maxSizePer);
}
代码示例来源:origin: google/guava
/**
* Ensures that this {@code CompactHashMap} has the smallest representation in memory, given its
* current size.
*/
public void trimToSize() {
int size = this.size;
if (size < entries.length) {
resizeEntries(size);
}
// size / loadFactor gives the table size of the appropriate load factor,
// but that may not be a power of two. We floor it to a power of two by
// keeping its highest bit. But the smaller table may have a load factor
// larger than what we want; then we want to go to the next power of 2 if we can
int minimumTableSize = Math.max(1, Integer.highestOneBit((int) (size / loadFactor)));
if (minimumTableSize < MAXIMUM_CAPACITY) {
double load = (double) size / minimumTableSize;
if (load > loadFactor) {
minimumTableSize <<= 1; // increase to next power if possible
}
}
if (minimumTableSize < table.length) {
resizeTable(minimumTableSize);
}
}
代码示例来源:origin: google/guava
/**
* Ensures that this {@code CompactHashSet} has the smallest representation in memory, given its
* current size.
*/
public void trimToSize() {
int size = this.size;
if (size < entries.length) {
resizeEntries(size);
}
// size / loadFactor gives the table size of the appropriate load factor,
// but that may not be a power of two. We floor it to a power of two by
// keeping its highest bit. But the smaller table may have a load factor
// larger than what we want; then we want to go to the next power of 2 if we can
int minimumTableSize = Math.max(1, Integer.highestOneBit((int) (size / loadFactor)));
if (minimumTableSize < MAXIMUM_CAPACITY) {
double load = (double) size / minimumTableSize;
if (load > loadFactor) {
minimumTableSize <<= 1; // increase to next power if possible
}
}
if (minimumTableSize < table.length) {
resizeTable(minimumTableSize);
}
}
代码示例来源: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 int floorPowerOfTwo(int x) {
checkPositive("x", x);
return Integer.highestOneBit(x);
}
代码示例来源:origin: bumptech/glide
private static int getSampleSize(GifHeader gifHeader, int targetWidth, int targetHeight) {
int exactSampleSize = Math.min(gifHeader.getHeight() / targetHeight,
gifHeader.getWidth() / targetWidth);
int powerOfTwoSampleSize = exactSampleSize == 0 ? 0 : Integer.highestOneBit(exactSampleSize);
// Although functionally equivalent to 0 for BitmapFactory, 1 is a safer default for our code
// than 0.
int sampleSize = Math.max(1, powerOfTwoSampleSize);
if (Log.isLoggable(TAG, Log.VERBOSE) && sampleSize > 1) {
Log.v(TAG, "Downsampling GIF"
+ ", sampleSize: " + sampleSize
+ ", target dimens: [" + targetWidth + "x" + targetHeight + "]"
+ ", actual dimens: [" + gifHeader.getWidth() + "x" + gifHeader.getHeight() + "]");
}
return sampleSize;
}
内容来源于网络,如有侵权,请联系作者删除!