Say we have the lexicographicaly integers 3,5,6,9,10,12 or 0011,0101,0110,1001,1010,1100
Each with two bits set.
What I want is to find the distance(how many lexicographical permutations between them, without doing the actuall permutations) between say 3
and 5
using as few operations as possible.
The distance table is as following
3->5 = 1 or 0011->0101 = 0001
3->6 = 2 or 0011->0110 = 0010
3->9 = 3 or 0011->1001 = 0011
3->10 = 4 or 0011->1010 = 0100
3->12 = 5 or 0011->1100 = 0101
So a function f(3,5) would return 1;
The function will always take arguments of same Hamming weight (same amount of set bits).
No arrays should be used.
Any idea would be great.
Edit
Forgot to mention, for any set bit size(the hamming weight) I will always use the first lexicographical permutation(base
) as the first argument.
E.g.
hamming weight 1 base = 1
hamming weight 2 base = 3
hamming weight 3 base = 7
...
Edit 2
The solution should work for any hamming weight, sorry I was not specific enough.