Invert bitwise shift operator / power of two

I have the following simple equation in my C # program to convert a number to a resulting value:

sectorSize = 1 << sectorShift; 

Is there some kind of reverse operation that will allow me to go the other way?

 sectorShift = ??? 

I know you can implement a loop, but this is a bit overkill. I have never had to do this before, so I have no idea and I can not find anything on the Internet about it. The equation I need is only for reliable results when the Size sector is a power of two; the rest of the domain can go to hell for everything I need.

+4
source share
2 answers

Logarithms But since you do not want to do this, use a table of loops and / or searches.

+9
source

Here are five ways to do this in C. Translating them to fix C # is left as an exercise. Be extremely careful.

http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious

Honestly, I personally would always go with a noose. I don’t understand why you think that the simple and clearly correct code is "overkill".

+11
source

Source: https://habr.com/ru/post/1403998/


All Articles