Welcome to little lamb

Code » test-msb » next » tree

[next] / src / m3.c

#include <stdint.h>

typedef uint64_t u64;

int msb(u64 v)
{
    if (!v) return 0;

    u64 msb  = 32;
    u64 step = msb;
    while (step > 1)
    {
        step /= 2;
        if (v >> msb)
            msb += step;
        else
            msb -= step;
    }
    if (v >> msb)
        ++msb;

    return msb;
}