在不使用Log()的情况下查找位

Ice*_*man 1 c# sql bit-manipulation

我有一个整数输入,功率为2(1,2,4,8等).我希望函数在不使用log()的情况下返回位位置.例如,对于上面的输入,将分别返回{0,1,2,3}对于C#.如果这可以在SQL中完成.

谢谢!

Wor*_*red 5

我的Mac上没有VS来测试它,但是你想要这样的东西吗?

public static int Foo(int n)
{
    if (n <= 0)
    {
        return -1; // A weird value is better than an infinite loop.
    }
    int i = 0;
    while (n % 2 == 0)
    {
        n /= 2;
        i++;
    }

    return i;
}
Run Code Online (Sandbox Code Playgroud)