question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add *Math.nextPowerOfTwo

See original GitHub issue

There are existing *Math.isPowerOfTwo methods, it seems like a nextPowerOfTwo method is a good method to go alongside it, to return the smallest integer >= n that is a power of two (I was surprised this doesn’t exist in guava already, TBH)

This is a simple operation that has a wide range of applications (sizing buffers, primarily, but it also has uses in collection implementations etc), and it’s not immediately obvious what the bit-twiddling algorithm is doing when it appears in a codebase. Would seem like a prime candidate for an addition to guava.

There’s a simple bit-twiddling algorithm for ints & longs; doubles & bigintegers can probably use a combination of the existing log, floor & pow methods, unless there are clever ways of doing the same for those types (also see http://stackoverflow.com/questions/466204/rounding-up-to-nearest-power-of-2)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
leventovcommented, Nov 8, 2018

@ben-manes thank you. Google (the search engine, not the company) is somehow bad at finding methods in published Guava’s Javadocs.

2reactions
ben-manescommented, Jan 27, 2016

A simple version of that is below, which is what I use. As its a one-liner I put the method in the class since its only rarely needed.

static int ceilingNextPowerOfTwo(int x) {
  // From Hacker's Delight, Chapter 3, Harry S. Warren Jr.
  return 1 << (Integer.SIZE - Integer.numberOfLeadingZeros(x - 1));
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mathf.NextPowerOfTwo - Scripting API - Unity - Manual
Returns the next power of two that is equal to, or greater than, the argument. using UnityEngine; public class ExampleClass : MonoBehaviour {...
Read more >
com.almasb.fxgl.core.math.FXGLMath.nextPowerOfTwo java code ...
Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many * items to avoid...
Read more >
Rounding up to next power of 2 - Stack Overflow
Check the Bit Twiddling Hacks. You need to get the base 2 logarithm, then add 1 to that. Example for a 32-bit value:...
Read more >
maths - JUCE: Class Index
int · nextPowerOfTwo (int n) noexcept. Returns the smallest power-of-two which is equal to or greater than the given integer.
Read more >
ee3de1e1f3da2386197a2c5863...
Remove undefined behavior from NextPowerOfTwo This CL removes undefined ... behavior of our invalid left shift borks the stack which causes the Math....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found