Numbers have a modular multiplicative inverse modulo a power of two precisely iff they are odd. Everything else is a bit-shifted odd number (even zero, which might be anything, with all bits shifted out). So there are a couple of cases:
Given a * x = b
tzcnt(a) > tzcnt(b)
no solution
tzcnt(a) <= tzcnt(b)
solvable, with 2tzcnt(a) solutions
The second case has a special case with 1 solution, for odd a
, namely x = inverse(a) * b
More generally, x = inverse(a >> tzcnt(a)) * (b >> tzcnt(a))
is a solution, because you write a
as (a >> tzcnt(a)) * (1 << tzcnt(a))
, so we cancel the left factor with its inverse, we leave the right factor as part of the result (cannot be cancelled anyway) and then multiply by what remains to get it up to b
. Still only works in the second case, obviously. If you wanted, you could enumerate all solutions by filling in all possibilities for the top tzcnt(a)
bits.
The only thing that remains is getting the inverse, you've probably seen it in the other answer, whatever it was, but for completeness you can compute it as follows: (not tested)
; input x
dword y = (x * x) + x - 1;
dword t = y * x;
y *= 2 - t;
t = y * x;
y *= 2 - t;
t = y * x;
y *= 2 - t;
; result y
0xcb9102df
, of course. (multiplicative inverses exist for division by small integers, like the div13 function I included in that godbolt link). – Kentigerma4*x=?2
, ? being the overflowed digit. Can I recover x? – Meghannmegiddo