With d wired to 2, you can use

    msks =: /:~@(> # ]) ~.@,@(+/~)@(0&,)@(i.@:>:@<.&.(2&^.))
    msks 64
0 1 2 3 4 5 6 8 9 10 12 16 17 18 20 24 32 33 34 36 40 48

If you need to change d, you can use

    msks =: (] #~ (>: +/"1...@#:)) i.
    2 msks 64
0 1 2 3 4 5 6 8 9 10 12 16 17 18 20 24 32 33 34 36 40 48
    3 msks 32
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 18 19 20 21 22 24 25 26 28


Henry Rich


Yuvaraj Athur Raghuvir wrote:
> Hello,
> 
> I want to rewrite Java Code[1] to J Code[2] . I want to check with the forum
> members if there is a better way to achieve this.
> 
> The issue is that in the setBits loop, the values are set using the
> previously set values. In the process of loop elimination, I found a J form
> and I hand coded the extension to the desired limit. The questions I have
> are:
> 1) For the J pattern I have found, is there a way to use ^: or $: forms?
> 2) Based on the math behind the Java Code, is there a different way to
> achieve this result? I am not able to formulate the problem so I did it the
> debugging way.
> 
> Thanks,
> Yuva
> 
> Java Code:
>     byte[] setBits = new byte[256];
>         int d  = 2;
>     for (int i = 0; i < 256; ++i) setBits[i] = (byte) ((i & 1) + setBits[i
>>> 1]);
>     //println(setBits);
>     // for every possible key mask...
>     masks = new int[0];
>     for (int i = 0; i < s; ++i)
>       // if d or fewer bits are set...
>       if (setBits[(i >> 24) & 0xFF] +
>           setBits[(i >> 16) & 0xFF] +
>           setBits[(i >>  8) & 0xFF] +
>           setBits[ i        & 0xFF] <= d)
>         // add it to the list of masks.
>         masks = append (masks, i);
> 
> My J Attempt:
> ls   =: (33 b.) NB. bit left shift
> and  =: (17 b.) NB. bit and
> or   =: (23 b.) NB. bit or
> exor =: (22 b.) NB. bitwise exclusive or
> 
> 
>  v0 =: 13 : ' (2&| i...@# y) + (2&| i...@#y)  {~ (_1&ls i...@#y) '
>  v1 =: 13 : ' (2&| i...@#y) + ( (-#y) {. (_1&ls i.+:@# y)) { y '
>  t2 =. (v0,v...@v0)  NB. t2 i.2 gives a string of length 4 on i.2
>  t3 =. (t2,v...@t2)  NB. 2^3
>  t4 =. (t3,v...@t3)  NB. 2^4
>  t5 =. (t4,v...@t4)  NB. 2^5
>  t6 =. (t5,v...@t5)  NB. 2^6
>  t7 =. (t6,v...@t6)  NB. 2^7
>  t8 =. (t7,v...@t7)  NB. 2^8
>  sb =. t8 i.2       NB. setBits
>  m0 =. (16bff&and)@:(0&ls + _8&ls + _16&ls + _24&ls) sb
>  m1 =. <:&2 m0
>  msk =: I. m1           NB. masks
> 
>    t2 i.2
> 0 1 1 2
>    t3 i.2
> 0 1 1 2 1 2 2 3
>    t4 i.2
> 0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
> 
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to