As you've specified x86, it sounds like you want the BSR
(bit-scan reverse) opcode, which reports the position of the most-significant set bit.
[FYI: big-O notation refers to asymptotic complexity (i.e. as N -> infinity); it doesn't make much sense if N has a finite limit (32 or 64 in this case).]