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

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

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

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

  1. /**
  2. * Calculates the minimum number of bits required to encode a value. This can
  3. * then in turn be used to calculate the number of septets or octets (as
  4. * appropriate) to use to encode a length parameter.
  5. *
  6. * @param value The value to calculate the minimum number of bits required to encode
  7. * @return The minimum number of bits required to encode the supplied value
  8. */
  9. private static int bitsToEncode(int value) {
  10. int highestOneBit = Integer.highestOneBit(value);
  11. int bitLength = 0;
  12. while ((highestOneBit >>= 1) != 0) {
  13. bitLength++;
  14. }
  15. return bitLength;
  16. }

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

  1. static int closedTableSize(int expectedEntries, double loadFactor) {
  2. // Get the recommended table size.
  3. // Round down to the nearest power of 2.
  4. expectedEntries = Math.max(expectedEntries, 2);
  5. int tableSize = Integer.highestOneBit(expectedEntries);
  6. // Check to make sure that we will not exceed the maximum load factor.
  7. if (expectedEntries > (int) (loadFactor * tableSize)) {
  8. tableSize <<= 1;
  9. return (tableSize > 0) ? tableSize : MAX_TABLE_SIZE;
  10. }
  11. return tableSize;
  12. }

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

  1. /**
  2. * Decrements the given number down to the closest power of two. If the argument is a
  3. * power of two, it remains unchanged.
  4. *
  5. * @param value The value to round down.
  6. * @return The closest value that is a power of two and less or equal than the given value.
  7. */
  8. public static int roundDownToPowerOf2(int value) {
  9. return Integer.highestOneBit(value);
  10. }

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

  1. private static int log2Ceiling(int value)
  2. {
  3. return Integer.highestOneBit(value - 1) << 1;
  4. }

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

  1. private static int expandedCapacity(int oldCapacity, int minCapacity) {
  2. if (minCapacity < 0) {
  3. throw new AssertionError("cannot store more than MAX_VALUE elements");
  4. }
  5. // careful of overflow!
  6. int newCapacity = oldCapacity + (oldCapacity >> 1) + 1;
  7. if (newCapacity < minCapacity) {
  8. newCapacity = Integer.highestOneBit(minCapacity - 1) << 1;
  9. }
  10. if (newCapacity < 0) {
  11. newCapacity = Integer.MAX_VALUE; // guaranteed to be >= newCapacity
  12. }
  13. return newCapacity;
  14. }

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

  1. private static int expandedCapacity(int oldCapacity, int minCapacity) {
  2. if (minCapacity < 0) {
  3. throw new AssertionError("cannot store more than MAX_VALUE elements");
  4. }
  5. // careful of overflow!
  6. int newCapacity = oldCapacity + (oldCapacity >> 1) + 1;
  7. if (newCapacity < minCapacity) {
  8. newCapacity = Integer.highestOneBit(minCapacity - 1) << 1;
  9. }
  10. if (newCapacity < 0) {
  11. newCapacity = Integer.MAX_VALUE; // guaranteed to be >= newCapacity
  12. }
  13. return newCapacity;
  14. }

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

  1. private static int expandedCapacity(int oldCapacity, int minCapacity) {
  2. if (minCapacity < 0) {
  3. throw new AssertionError("cannot store more than MAX_VALUE elements");
  4. }
  5. // careful of overflow!
  6. int newCapacity = oldCapacity + (oldCapacity >> 1) + 1;
  7. if (newCapacity < minCapacity) {
  8. newCapacity = Integer.highestOneBit(minCapacity - 1) << 1;
  9. }
  10. if (newCapacity < 0) {
  11. newCapacity = Integer.MAX_VALUE; // guaranteed to be >= newCapacity
  12. }
  13. return newCapacity;
  14. }

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

  1. static int expandedCapacity(int oldCapacity, int minCapacity) {
  2. if (minCapacity < 0) {
  3. throw new AssertionError("cannot store more than MAX_VALUE elements");
  4. }
  5. // careful of overflow!
  6. int newCapacity = oldCapacity + (oldCapacity >> 1) + 1;
  7. if (newCapacity < minCapacity) {
  8. newCapacity = Integer.highestOneBit(minCapacity - 1) << 1;
  9. }
  10. if (newCapacity < 0) {
  11. newCapacity = Integer.MAX_VALUE;
  12. // guaranteed to be >= newCapacity
  13. }
  14. return newCapacity;
  15. }

代码示例来源:origin: bumptech/glide

  1. @Override
  2. public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
  3. int requestedHeight) {
  4. int minIntegerFactor = Math.min(sourceHeight / requestedHeight, sourceWidth / requestedWidth);
  5. return minIntegerFactor == 0 ? 1f : 1f / Integer.highestOneBit(minIntegerFactor);
  6. }

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

  1. /**
  2. * Returns an array size suitable for the backing array of a hash table that uses open addressing
  3. * with linear probing in its implementation. The returned size is the smallest power of two that
  4. * can hold setSize elements with the desired load factor.
  5. */
  6. @VisibleForTesting
  7. static int chooseTableSize(int setSize) {
  8. if (setSize == 1) {
  9. return 2;
  10. }
  11. // Correct the size for open addressing to match desired load factor.
  12. // Round up to the next highest power of 2.
  13. int tableSize = Integer.highestOneBit(setSize - 1) << 1;
  14. while (tableSize * DESIRED_LOAD_FACTOR < setSize) {
  15. tableSize <<= 1;
  16. }
  17. return tableSize;
  18. }

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

  1. static int closedTableSize(int expectedEntries, double loadFactor) {
  2. // Get the recommended table size.
  3. // Round down to the nearest power of 2.
  4. expectedEntries = Math.max(expectedEntries, 2);
  5. int tableSize = Integer.highestOneBit(expectedEntries);
  6. // Check to make sure that we will not exceed the maximum load factor.
  7. if (expectedEntries > (int) (loadFactor * tableSize)) {
  8. tableSize <<= 1;
  9. return (tableSize > 0) ? tableSize : MAX_TABLE_SIZE;
  10. }
  11. return tableSize;
  12. }

代码示例来源:origin: bumptech/glide

  1. @Override
  2. public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
  3. int requestedHeight) {
  4. int maxIntegerFactor = (int) Math.ceil(Math.max(sourceHeight / (float) requestedHeight,
  5. sourceWidth / (float) requestedWidth));
  6. int lesserOrEqualSampleSize = Math.max(1, Integer.highestOneBit(maxIntegerFactor));
  7. int greaterOrEqualSampleSize =
  8. lesserOrEqualSampleSize << (lesserOrEqualSampleSize < maxIntegerFactor ? 1 : 0);
  9. return 1f / greaterOrEqualSampleSize;
  10. }

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

  1. /**
  2. * Returns the largest power of two less than or equal to {@code x}. This is equivalent to {@code
  3. * checkedPow(2, log2(x, FLOOR))}.
  4. *
  5. * @throws IllegalArgumentException if {@code x <= 0}
  6. * @since 20.0
  7. */
  8. @Beta
  9. public static int floorPowerOfTwo(int x) {
  10. checkPositive("x", x);
  11. return Integer.highestOneBit(x);
  12. }

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

  1. /**
  2. * Returns an array size suitable for the backing array of a hash table that uses open addressing
  3. * with linear probing in its implementation. The returned size is the smallest power of two that
  4. * can hold setSize elements with the desired load factor. Always returns at least setSize + 2.
  5. */
  6. @VisibleForTesting
  7. static int chooseTableSize(int setSize) {
  8. setSize = Math.max(setSize, 2);
  9. // Correct the size for open addressing to match desired load factor.
  10. if (setSize < CUTOFF) {
  11. // Round up to the next highest power of 2.
  12. int tableSize = Integer.highestOneBit(setSize - 1) << 1;
  13. while (tableSize * DESIRED_LOAD_FACTOR < setSize) {
  14. tableSize <<= 1;
  15. }
  16. return tableSize;
  17. }
  18. // The table can't be completely full or we'll get infinite reprobes
  19. checkArgument(setSize < MAX_TABLE_SIZE, "collection too large");
  20. return MAX_TABLE_SIZE;
  21. }

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

  1. public FSTIdentity2IdMap(int initialSize) {
  2. if (initialSize < 2) {
  3. initialSize = 2;
  4. }
  5. initialSize = adjustSize(initialSize * GROFAC);
  6. mKeys = new Object[initialSize];
  7. mValues = new int[initialSize];
  8. mNumberOfElements = 0;
  9. mask = (Integer.highestOneBit(initialSize) << 1) - 1;
  10. klen = initialSize - 4;
  11. }

代码示例来源:origin: apache/incubator-druid

  1. public static int maxIntsInBufferForBytes(int numBytes)
  2. {
  3. int maxSizePer = (CompressedPools.BUFFER_SIZE - bufferPadding(numBytes)) / numBytes;
  4. // round down to the nearest power of 2
  5. return Integer.highestOneBit(maxSizePer);
  6. }

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

  1. /**
  2. * Ensures that this {@code CompactHashMap} has the smallest representation in memory, given its
  3. * current size.
  4. */
  5. public void trimToSize() {
  6. int size = this.size;
  7. if (size < entries.length) {
  8. resizeEntries(size);
  9. }
  10. // size / loadFactor gives the table size of the appropriate load factor,
  11. // but that may not be a power of two. We floor it to a power of two by
  12. // keeping its highest bit. But the smaller table may have a load factor
  13. // larger than what we want; then we want to go to the next power of 2 if we can
  14. int minimumTableSize = Math.max(1, Integer.highestOneBit((int) (size / loadFactor)));
  15. if (minimumTableSize < MAXIMUM_CAPACITY) {
  16. double load = (double) size / minimumTableSize;
  17. if (load > loadFactor) {
  18. minimumTableSize <<= 1; // increase to next power if possible
  19. }
  20. }
  21. if (minimumTableSize < table.length) {
  22. resizeTable(minimumTableSize);
  23. }
  24. }

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

  1. /**
  2. * Ensures that this {@code CompactHashSet} has the smallest representation in memory, given its
  3. * current size.
  4. */
  5. public void trimToSize() {
  6. int size = this.size;
  7. if (size < entries.length) {
  8. resizeEntries(size);
  9. }
  10. // size / loadFactor gives the table size of the appropriate load factor,
  11. // but that may not be a power of two. We floor it to a power of two by
  12. // keeping its highest bit. But the smaller table may have a load factor
  13. // larger than what we want; then we want to go to the next power of 2 if we can
  14. int minimumTableSize = Math.max(1, Integer.highestOneBit((int) (size / loadFactor)));
  15. if (minimumTableSize < MAXIMUM_CAPACITY) {
  16. double load = (double) size / minimumTableSize;
  17. if (load > loadFactor) {
  18. minimumTableSize <<= 1; // increase to next power if possible
  19. }
  20. }
  21. if (minimumTableSize < table.length) {
  22. resizeTable(minimumTableSize);
  23. }
  24. }

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

  1. /**
  2. * Returns the largest power of two less than or equal to {@code x}. This is equivalent to {@code
  3. * checkedPow(2, log2(x, FLOOR))}.
  4. *
  5. * @throws IllegalArgumentException if {@code x <= 0}
  6. * @since 20.0
  7. */
  8. @Beta
  9. public static int floorPowerOfTwo(int x) {
  10. checkPositive("x", x);
  11. return Integer.highestOneBit(x);
  12. }

代码示例来源:origin: bumptech/glide

  1. private static int getSampleSize(GifHeader gifHeader, int targetWidth, int targetHeight) {
  2. int exactSampleSize = Math.min(gifHeader.getHeight() / targetHeight,
  3. gifHeader.getWidth() / targetWidth);
  4. int powerOfTwoSampleSize = exactSampleSize == 0 ? 0 : Integer.highestOneBit(exactSampleSize);
  5. // Although functionally equivalent to 0 for BitmapFactory, 1 is a safer default for our code
  6. // than 0.
  7. int sampleSize = Math.max(1, powerOfTwoSampleSize);
  8. if (Log.isLoggable(TAG, Log.VERBOSE) && sampleSize > 1) {
  9. Log.v(TAG, "Downsampling GIF"
  10. + ", sampleSize: " + sampleSize
  11. + ", target dimens: [" + targetWidth + "x" + targetHeight + "]"
  12. + ", actual dimens: [" + gifHeader.getWidth() + "x" + gifHeader.getHeight() + "]");
  13. }
  14. return sampleSize;
  15. }

相关文章