I need get the exactly offset for a int, something like below:
1 => 0
2 => 1
4 => 2
8 => 3
Assuming the input must be 2^n (0<= n <= 20), Is there a 0(1) algorithm for it?
I need get the exactly offset for a int, something like below:
1 => 0
2 => 1
4 => 2
8 => 3
Assuming the input must be 2^n (0<= n <= 20), Is there a 0(1) algorithm for it?
Sure
Unity.Mathematics.math.tzcnt(x)
Even just looping over all 20 possible positions is also O(1) since 20 is a constant.