You need to use Marshall's two's complement inverse
tcinv
[: #. (* (_1 ^ 0 = i.@#))"1
tcinv tc i: 6
_6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6
In emails I insert an extra space in front of J output -- this seems to
protect it from the vagaries of email.
I will look at your post, thanks for repeating it.
On 12/16/2011 2:40 AM, Linda Alvord wrote:
> Here's what I get. The line will split but when I paste it back together:
>
> tc =: #:~ 2 #~ [:>./ 1 + [:>. (2 ^. |)`(2 ^.>:)@.(0<: ])"0
> ]a=:tc i:6
> 1 0 1 0
> 1 0 1 1
> 1 1 0 0
> 1 1 0 1
> 1 1 1 0
> 1 1 1 1
> 0 0 0 0
> 0 0 0 1
> 0 0 1 0
> 0 0 1 1
> 0 1 0 0
> 0 1 0 1
> 0 1 1 0
> #.a
> 10 11 12 13 14 15 0 1 2 3 4 5 6
>
> Not good. But your display does not have the first column, so maybe you need
> to post the function again.
>
> I tried to remove the first column to match your dislay:
>
> ]b=:1}."1 a
> 0 1 0
> 0 1 1
> 1 0 0
> 1 0 1
> 1 1 0
> 1 1 1
> 0 0 0
> 0 0 1
> 0 1 0
> 0 1 1
> 1 0 0
> 1 0 1
> 1 1 0
>
> #.b
> 2 3 4 5 6 7 0 1 2 3 4 5 6
>
> This is also not working.
>
> Did you really read my post in a reply to a message to Devon? I'll repeat it
> here as I am interested in your comments:
>
> ]a=:8+i.15
> 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
> ]b=:15#15
> 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
> ]c=:a-b
> _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7
> f=: 13 :'(x#2)#:y'
> f
> ] #:~ 2 #~ [
>
> The function f converts the lists to binary numbers. The largest number 22
> is less than 32 so only 5 digits are require.
>
> ]aa=:5 f a
> 0 1 0 0 0
> 0 1 0 0 1
> 0 1 0 1 0
> 0 1 0 1 1
> 0 1 1 0 0
> 0 1 1 0 1
> 0 1 1 1 0
> 0 1 1 1 1
> 1 0 0 0 0
> 1 0 0 0 1
> 1 0 0 1 0
> 1 0 0 1 1
> 1 0 1 0 0
> 1 0 1 0 1
> 1 0 1 1 0
>
> ]bb=:5 f b
> 0 1 1 1 1
> 0 1 1 1 1
> 0 1 1 1 1
> 0 1 1 1 1
> 0 1 1 1 1
> 0 1 1 1 1
> 0 1 1 1 1
> 0 1 1 1 1
> 0 1 1 1 1
> 0 1 1 1 1
> 0 1 1 1 1
> 0 1 1 1 1
> 0 1 1 1 1
> 0 1 1 1 1
> 0 1 1 1 1
>
> The result below is the difference in J between the two arrays above. It
> is not a conversion using #:
>
> ]cc=:aa-bb
> 0 0 _1 _1 _1
> 0 0 _1 _1 0
> 0 0 _1 0 _1
> 0 0 _1 0 0
> 0 0 0 _1 _1
> 0 0 0 _1 0
> 0 0 0 0 _1
> 0 0 0 0 0
> 1 _1 _1 _1 _1
> 1 _1 _1 _1 0
> 1 _1 _1 0 _1
> 1 _1 _1 0 0
> 1 _1 0 _1 _1
> 1 _1 0 _1 0
> 1 _1 0 0 _1
>
> For comparison, the result cc2 is obtained as a J result using #:
> ]cc2=: 5 f c
> 1 1 0 0 1
> 1 1 0 1 0
> 1 1 0 1 1
> 1 1 1 0 0
> 1 1 1 0 1
> 1 1 1 1 0
> 1 1 1 1 1
> 0 0 0 0 0
> 0 0 0 0 1
> 0 0 0 1 0
> 0 0 0 1 1
> 0 0 1 0 0
> 0 0 1 0 1
> 0 0 1 1 0
> 0 0 1 1 1
>
> Or:
>
> ]cc3=:#:c
> 0 0 1
> 0 1 0
> 0 1 1
> 1 0 0
> 1 0 1
> 1 1 0
> 1 1 1
> 0 0 0
> 0 0 1
> 0 1 0
> 0 1 1
> 1 0 0
> 1 0 1
> 1 1 0
> 1 1 1
>
> Both cc2 and cc3 seem implausible because all numbers are known positive
> numbers. Now look at cc :
>
> w=: 13 :'(x,y)$|.2^i.y'
> w
> , $ [: |. 2 ^ [: i. ]
> 15 w 5
> 16 8 4 2 1
> 16 8 4 2 1
> 16 8 4 2 1
> 16 8 4 2 1
> 16 8 4 2 1
> 16 8 4 2 1
> 16 8 4 2 1
> 16 8 4 2 1
> 16 8 4 2 1
> 16 8 4 2 1
> 16 8 4 2 1
> 16 8 4 2 1
> 16 8 4 2 1
> 16 8 4 2 1
> 16 8 4 2 1
>
> cc*15 w 5
> 0 0 _4 _2 _1
> 0 0 _4 _2 0
> 0 0 _4 0 _1
> 0 0 _4 0 0
> 0 0 0 _2 _1
> 0 0 0 _2 0
> 0 0 0 0 _1
> 0 0 0 0 0
> 16 _8 _4 _2 _1
> 16 _8 _4 _2 0
> 16 _8 _4 0 _1
> 16 _8 _4 0 0
> 16 _8 0 _2 _1
> 16 _8 0 _2 0
> 16 _8 0 0 _1
>
>
> +/"1 cc*15 w 5
> _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7
>
> This is c!
>
> Not only that, J knows it is c.
>
> #.cc
> _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7
>
> So what do you think?
>
> Linda
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Kip Murray
> Sent: Friday, December 16, 2011 2:43 AM
> To: Programming forum
> Subject: Re: [Jprogramming] How #: should have been designed
>
> The verb tc (two's complement) appears to work
>
> tc =: #:~ 2 #~ [:>./ 1 + [:>. (2 ^. |)`(2 ^.>:)@.(0<: ])"0
>
> twoscomplement i: 6
> 0 1 0
> 0 1 1
> 1 0 0
> 1 0 1
> 1 1 0
> 1 1 1
> 0 0 0
> 0 0 1
> 0 1 0
> 0 1 1
> 1 0 0
> 1 0 1
> 1 1 0
>
> tc i: 6
> 1 0 1 0
> 1 0 1 1
> 1 1 0 0
> 1 1 0 1
> 1 1 1 0
> 1 1 1 1
> 0 0 0 0
> 0 0 0 1
> 0 0 1 0
> 0 0 1 1
> 0 1 0 0
> 0 1 0 1
> 0 1 1 0
>
>
> On 12/15/2011 12:52 PM, Kip Murray wrote:
>> twoscomplement i: 4 NB. error
>> 1 0 0
>> 1 0 1
>> 1 1 0
>> 1 1 1
>> 0 0 0
>> 0 0 1
>> 0 1 0
>> 0 1 1
>> 1 0 0
>>
>> rp =: [: }: [: i: 2 ^<: NB. representable in y bits
>>
>> rp 3
>> _4 _3 _2 _1 0 1 2 3
>>
>> twoscomplement rp 3 NB. OK
>> 1 0 0
>> 1 0 1
>> 1 1 0
>> 1 1 1
>> 0 0 0
>> 0 0 1
>> 0 1 0
>> 0 1 1
>>
>>
>> On 12/14/2011 9:13 AM, Raul Miller wrote:
>>> The subject line of this thread is arguably wrong -- there are a
>>> variety of "good ways" of decomposing integers to binary.
>>>
>>> That said, it's interesting to think about the various proposals
>>> expressed in terms similar to those which could be used to implement
>>> monadic #:
>>>
>>> antibase2=: #:~ 2 #~ 1 + 2<.@^. 1>.>./@,@:|@:<.
>>> twoscomplement=: #:~ 2 #~ 1 + 2<.@^. 1 +>./@,@:|@:<.
>>> signwithbits=: #:~ 0, 2 #~ 1 + 2<.@^. 1>.>./@,@:|@:<.
>>>
>>> (In all cases the #: here is dyadic, so these definitions are
>>> independent of the definition of monadic #:)
>>>
>>> antibase2 i: 3
>>> 0 1
>>> 1 0
>>> 1 1
>>> 0 0
>>> 0 1
>>> 1 0
>>> 1 1
>>> twoscomplement i: 3
>>> 1 0 1
>>> 1 1 0
>>> 1 1 1
>>> 0 0 0
>>> 0 0 1
>>> 0 1 0
>>> 0 1 1
>>> signwithbits i: 3
>>> _1 0 1
>>> _1 1 0
>>> _1 1 1
>>> 0 0 0
>>> 0 0 1
>>> 0 1 0
>>> 0 1 1
>>>
>>> There's also (* * #:) but that one assumes the antibase2
>>> implementation...
>>>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm