Hiya, say I'm given a number in the range 0-1024, what's the easiest way to round it up to the next power of 2 value (aside from having an array (or loop) or ^2 values and comparing the given value against them). Thanks!
what's the easiest way to round up to power of 2?
BlitzMax Forums/BlitzMax Programming/what's the easiest way to round up to power of 2? what a handy function! Thanks TonyG, glad to see you are on the ball this morning.
Yeah just thought theere might be a fancy maths way without a loop.
Hey I that funciton won't compile, which module is it in?
Yeah just thought theere might be a fancy maths way without a loop.
Hey I that funciton won't compile, which module is it in?
Its no function that you can access, its a function within one of the GL functions (I think the gltexsize calculation function)
But you can easily put it into your own files or into the function that actually needs this function :-)
PS: YES, BM has some strange features like functions within methods and functions ^^
But you can easily put it into your own files or into the function that actually needs this function :-)
PS: YES, BM has some strange features like functions within methods and functions ^^
I'll make it into a common code function then, thx.
I've used this one for my texturegenerator:
Dunno if it's any faster or slower. I'm not sure whether it's actually the correct solution either .. it finds the biggest power of 2 from a value. :P
Function BPo2:Int(v:Int) If v<0 v=0 Local c:Int=1 While v>0 v=v Shr 1 c=c Shl 1 Wend Return c Shr 1 End Function
Dunno if it's any faster or slower. I'm not sure whether it's actually the correct solution either .. it finds the biggest power of 2 from a value. :P
Without a loop.
Get the square root of your number. Round up the result and square it.
Square root is a slow process, so I have no idea if this would be faster or slower.
Get the square root of your number. Round up the result and square it.
Square root is a slow process, so I have no idea if this would be faster or slower.
TaskMaster, that won't give you a power of 2, it'll give you a perfect square. A power of 2 is a number of the form 2^x, not x^2.
Whoops, yep. What was I thinking. LOL
If the range is fixed, and you want speed, wouldn't a series of if (x >= n and x < m) statements for each part of the range be the fatest solution?
dmaz: Nice. Mind you I don't actually need speed :-)