ISourceCode

Make the frequent cases fast and the rare case correct

One liner to find nearest power of 2 for a given number

For a given number , for eg 5 the nearest power of 2 is 8. for 11 it is 16 and so on.

This one liner finds the nearest power of 2.

(unsigned) pow( 2, ceil( log( n ) / log( 2 ) ) );

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: