Re: fastest transition from bitmask to the position of it's lowest SET bit



On May 29, 9:40 am, Lars Uffmann <a...@xxxxxxxxxxxxxx> wrote:
For example:
V = b0110101000110010 (2 bytes)
M = b0000011111110000 (2 bytes)
O = ln (16) / ln (2) = 4

P = b0110101000110010
AND b0000011111110000
SHR 4
= b0000001000110000 SHR 4 = b0000000000100011 = b100011 (= d35)

Now I am quite at a loss to determine that order O in a single
mathematical operation other than the logarithm.

You want (V & M) / (M & -M):

V = b0110101000110010
M = b0000011111110000
-M = b1111100000010000 <-- 2's complement
M&-M = b0000000000010000 <-- lowest bit on
V&M = b0000001000110000 <-- masked
/M&-M = b0000000000100011 <-- divide is like shr low_bit_pos

Jason



.