I have an int parameter with the possible values 1,2,4,8,16,32,64.
I need to know the bit offset of the current value, i.e. for each value return 1, 2, 3, 4, 5, or 6 respectively.
What is the easiest way to achieve that?
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.
You have multiple answers here : http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
the easiest being, assuming you have your input value in an unsigned int v :
but it will change v in the process.
edit: in your case if you are 100% sure your input is and int and a power of 2, a look-up-table may be the simplest and fastest