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
111010100110000
So it needs 15 bits
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For more advanced methods, see here http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
Note that this computes the index of the leftmost set bit (14 for 30000). If you want the number of bits, just add 1.