Minimum number of bits to represent number

What is the most efficient way to find out how many bits are needed to represent some random int number? For example number 30,000 is represented binary with


So it needs 15 bits


int v = 30000; // 32-bit word to find the log base 2 of
int r = 0; // r will be lg(v)

while ( (v >>= 1) != 0) // unroll for more speed...

For more advanced methods, see here

Note that this computes the index of the leftmost set bit (14 for 30000). If you want the number of bits, just add 1.

Need Your Help

Github - How to return the whole project to a certain commit in Webstorm

git github webstorm

I am using Github with Webstorm- I am the only person who is using this project.

Installing poco library

c++ poco-libraries

im trying to install the poco library for visual c++ 2008 but when I type this command

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.