How do I write this C expression in J? (where x
is input integer, and a
is temporary variable)
((a= ~x & (~x >> 1)) ^= a ? 0 : (a ^ (a & (a - 1))) | (a ^ (a & (a - 1))) << 1);
.
In a more readable form:
int a = (~x) & ((~x) >> 1);
if (a == 0) return 0;
int b = a ^ (a & (a - 1));
return b | (b << 1);
Without testing, the basic transcription would be something like this:
Shift =: (33 b.)
And =: (17 b.)
Not =: (26 b.)
Xor =: (22 b.)
Or =: (23 b.)
BastardFunction =: 3 : 0
a =. (Not y) And (_1 Shift (Not y))
if. a do.
b =. a Xor (a And a - 1)
(1 Shift b) Or b
else.
0
end.
)
But there could be a smarter approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With