On 8/28/07, Andrew Nikitin <[EMAIL PROTECTED]> wrote:
> Ralph, I am not sure if you know this trick or not, but if you start with
> interger (n) whose binary representation contains exactly k ones then
> { int t,b;
> t=n^(n&(n-1));
> b=t+n;
> n=b|(((b^n)/t)>>2);
> }
> generates next larger number with exactly k ones. As long as you stay within
> int size (in your case with 28 elements you do) it is a decent method.
Here's a J translation:
bits=: &.((64#2)&#:)
rot =: |.!.0
nikitin=:3 :0
t=. y ~:bits y *:bits y-1
b=. t + y
b +.bits _2&rot bits (b ~:bits y) <[EMAIL PROTECTED] t
)
(,:nikitin"0)i.8
0 1 2 3 4 5 6 7
0 2 4 6 8 10 12 14
Have I done something wrong? From your description,
I would have expected
0 1 2 3 4 5 6 7
0 2 4 5 8 6 9 14
Thanks,
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm