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