math - how to round an odd integer towards the nearest power of two -


add 1 or subtract 1 odd integer such result closer nearest power of two.

if ( ??? ) x += 1; else x -= 1;// x > 2 , odd 

for example, 25 through 47 round towards 32, adding 1 25 through 31 , subtracting 1 33 through 47. 23 rounds down towards 16 22 , 49 rounds towards 64 50.

is there way without finding specific power of 2 being rounded towards. know how use logarithm or count bits specific power of two.

my specific use case in splitting odd sized inputs karatsuba multiplication.

if second significant bit set add, otherwise subtract.

if ( (x&(x>>1)) > (x>>2) ) x += 1; else x -= 1;


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -