I am going over the messages in this thread slowly, because this
subject requires some thought. But that also means that some of my
responses in this thread will appear slowly.

Anyways:

On Thu, Jan 30, 2014 at 1:02 PM, Pascal Jasmin <godspiral2...@yahoo.ca> wrote:
> http://repl.it/languages/Python is a useful resource for figuring out python 
> code.  But here is the only part I had trouble understanding:
>
> leftmostbit =: 2&#.@:({. , 0 $~ 2 -~ #)@:(2&#. inv) NB. for some reason 
> divides msb by 2.

Here was the version I came up with:

   leftmostBit=: </\&.#:

If I compare this to the python code:

    def leftmost_bit( x ):
      assert x > 0
      result = 1
      while result <= x: result = 2 * result
      return result // 2

The python variable 'result' is indeed divided by 2 (// in python is
like <.@% in J), but notice that there's an extra 2 * result in the
loop when result is equal to x.

Example use:
   leftmostBit"0 i.10
0 1 2 2 4 4 4 4 8 8

And here's the comparable python (leaving out 0 because of the assert):
   [leftmost_bit(x) for x in range(1,10)]
[1, 2, 2, 4, 4, 4, 4, 8, 8]

Anyways, python is a bit quirky (but maybe that is a characteristic of
any computer system), but inspecting the data can work there just as
it can work in J.

Thanks,

-- 
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to