Note that neither implementation handled complex numbers.
To deal with complex numbers, I think I would use:
((2<.@^.1>.>./@,@:((2 * |@+. - 1:)^:(-.@-: |))) ((%&2@-&{.,])2&|)@]^:["1 ,"0)
...except that needs to be one line. Alternatively, here's a
variation with some concepts broken out and labeled:
signed=: -.@-: |
effective_magnitude=: (2 * |@+. - 1:)^:signed
bit_count=: 2 <.@^. 1>. >./@,@:effective_magnitude
extract_bit=: (%&2@-&{. , ]) 2&|
binary=: bit_count extract_bit@]^:["1 ,"0
--
Raul
On Tue, Dec 27, 2011 at 11:16 AM, Raul Miller <[email protected]> wrote:
> Best I can come up with at the moment is
> (([:<.2^.1>.>./@,) (<.@%&2@{.,2&|)@]^:["1 ,"0)
>
> Note that this version does not handle negative numbers. If I wanted
> that, I would probably use (except not split across two lines):
>
> (([:<.2^.1>.>./@,@:((2 * | - 1:)^:(<&0))) (<.@%&2@{.,2&|)@]^:["1 ,"0)
>
> This is a different result than the #: monad gives, for negative
> numbers. However, this result is compatible with #. and getting the
> twos complement representation from this result is trivial.
>
> --
> Raul
>
> On Tue, Dec 27, 2011 at 7:13 AM, Kip Murray <[email protected]> wrote:
>> Can you write a verb br which returns the binary representation of y without
>> using #: or #. ?
>>
>> br 0
>> 0
>> br 2
>> 1 0
>> br i. 5
>> 0 0 0
>> 0 0 1
>> 0 1 0
>> 0 1 1
>> 1 0 0
>> br _13
>> |domain error: br
>> | br _13
>>
>> An inverse I like is
>>
>> bv =: (2 p.~ |.)"1 :. br
>>
>> bv br i. 5
>> 0 1 2 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