1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#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; }