Here are my latest thoughts on negative binary numbers.

   a=:1 0 0 1 1 0 1
   #.a
77
   b=:-a
   b
_1 0 0 _1 _1 0 _1
   #.b
_77

The challenge is to get from  _77  to  _1 0 0 _1 _1 0 _1   which should be
the result that  #:  provides. But that is not what happens:

   #:#.b
0 1 1 0 0 1 1
   #.#:#.b
51
   
So here is a script which does that. It has lots of possibilities for
simplification but it does give an idea of a possible approach. If someone
can write the parts in loops or recursive style it would be interesting to
compare results. I'm not a J programmer, but rather a J user who would like
#:  to work.
Have fun, share your ideas.


]n=:15
NB.Number of places like 4 for a 4 digit number
d=: 13 :'-?y#2' 
d
]dd=:d n
NB.  d  creates an  n  digit negative binary number 
w=: 13 :'2^|.i.y'
w
w n
NB. w  is a weighting list for each place value
]nd=:#.dd
NB.  nd  converts a negative binary to a negative decimaldecimal number
NB.nd=:_432532   insert a specific decimal to convert it
]p=:+/nd <-w 20
]b1=:-1=<.nd%-w n
]r1=:nd-#.b1
]b2=:-1=<.r1%-w n
]r2=:nd-#.b1+b2
]b3=:-1=<.r2%-w n
]r3=:nd-#.b1+b2+b3
]b4=:-1=<.r3%-w n
]r4=:nd-#.b1+b2+b3+b4
]b5=:-1=<.r4%-w n
]r5=:nd-#.b1+b2+b3+b4+b5
]b6=:-1=<.r5%-w n
]r6=:nd-#.b1+b2+b3+b4+b5+b6
]b7=:-1=<.r6%-w n
]r7=:nd-#.b1+b2+b3+b4+b5+b6+b7
]b8=:-1=<.r7%-w n
]r8=:nd-#.b1+b2+b3+b4+b5+b6+b7+b8
]b9=:-1=<.r8%-w n
]nb=:b1+b2+b3+b4+b5+b6+b7+b8+b9
#.nb
NB. nb has created the correct negative binary number
]dd
#.dd
]p
NB.nb id the binary created from a negative decimal number
#:nd
#.#:nd
NB. results with problems

Linda



-----Original Message-----
From: programming-boun...@jsoftware.com
[mailto:programming-boun...@jsoftware.com] On Behalf Of Kip Murray
Sent: Saturday, December 17, 2011 2:02 AM
To: Programming forum
Subject: Re: [Jprogramming] How #: should have been designed

Linda, if you look over my original post, shown way below, you will see 
I used a verb of Raul's named twoscomplement and then a verb of mine 
named tc.  Don't overlook Raul's part at the very bottom.  Raul has 
since presented a corrected version of twoscomplement.  It appears to me 
what you got from my verb tc is the same as what I got.  I will think 
over your work below, in particular the part about f and #.f .  Kip

On 12/16/2011 9:16 PM, Linda Alvord wrote:
>
> Kip, I get a different result from  tc  than you show in your original
post.
> Using J  I get:
>
>     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
>
> This is copied from your post:
>
> 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
>
> Call this b:
>
>     ]b=: }."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
>
>   c=:#:i:6
>     c
> 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 tc i:6)-:(#:i:6)
> 1
>     b-:c
> 1
>      tcinv=:[: #. (* (_1 ^ 0= i.@#))"1
>      tcinv a
> _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6
>
>     tcinv b
> 2 3 _4 _3 _2 _1 0 1 2 3 _4 _3 _2
>
> So I thought you  really meant the four column result in  a  .
>
> However the array in  b matches the result of using  #: on  i:
>
>     ]c=:#: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
>
>     b-:c
> 1
>
>     No matter if it was created by #: or by  tc  , it is an array of
unique
> numbers.  It only represents the following list.
>
>      #.b
> 2 3 4 5 6 7 0 1 2 3 4 5 6
>     #.c
> 2 3 4 5 6 7 0 1 2 3 4 5 6
>
> This is incorrect.
>
> Now consider:
>
>     (8+i.15)-15#15
> _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7
>
>     d=:(#:8+i.15)-((5#2)#:,15#15)
>     d
> 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
>
> The array in  d is a beautiful representation of negative binary numbers.
> They include negative signs. If the leftmost digit is negative, the number
> is negative.  Look only at the top and if you recognize the pattern as 5
it
> will
> Be negative  5
>
> The array follows traditional place value.
>
>    |.2^i.5
> 16 8 4 2 1
>
>     d*($d)$|.2^i.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  d*($d)$|.2^i.5
> _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7
>
> And the sum of the value of the digits is correct.  Furthermore,  #.
> Understands it with no change in code.
>
>     #.d
> _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7
>
>
> My contention is that if we, the users, could get used to negative
numbers,
> as we have learned to like  _123.132j432.23 we would embrace these six
> binary representations:
>
>     f=:0,.0,.(?6 7$3){_1 0 1
>     f
> 0 0 _1  1  1  0 _1 _1  1
> 0 0  1 _1  0  1  1 _1  0
> 0 0  0 _1 _1 _1 _1  1 _1
> 0 0  0  0 _1  1  0  0  1
> 0 0  1  0 _1 _1  0  0  0
> 0 0  1 _1  1 _1  1  0  1
>
> The two columns of  0's  are necessary for this to work correctly.
>
> Using place value and J we can "read" these numbers correctly
>
>     f*($f)$|.2^i.9
>
> 0 0 _64  32  16  0 _4 _2  1
> 0 0  64 _32   0  8  4 _2  0
> 0 0   0 _32 _16 _8 _4  2 _1
> 0 0   0   0 _16  8  0  0  1
> 0 0  64   0 _16 _8  0  0  0
> 0 0  64 _32  16 _8  4  0  1
>
>     +/"1 f*($f)$|.2^i.9
> _21 42 _59 _7 40 45
>
>   And with no change in code,  #.  Gives the same results
>
>     #.f
> _21 42 _59 _7 40 45
>
> It is only  #:  that must be changed. I would think this would be fairly
> straightforward if these representatives of negative binary numbers were
> adopted.
>
>     #:#.f
>
> 1 1 1 1 1 0 0
> 0 1 0 0 0 1 0
> 0 1 1 1 1 0 1
> 1 0 1 0 0 1 0
> 0 1 1 1 0 1 1
> 1 1 0 0 0 1 0
>
> Since the current representation that  #: provides has only positive
numbers
> it is where the changes are necessary.
>
> Finally ou would never see the displays above if  you only consider
positive
> integers.
>
>     ]e=:#:i.7
> 0 0 0
> 0 0 1
> 0 1 0
> 0 1 1
> 1 0 0
> 1 0 1
> 1 1 0
>     #.e
> 0 1 2 3 4 5 6
>
> So, my take on this is that the user must learn to accept a new way of
> understanding of what binary numbers look like.  They will be unique. You
> can identify the sign by the leading element. If you can read the number
in
> binary, it will be the same number but negative.
>
> I keep trying to explain, and as I do it is getting easier for me to
> understand what I am saying.  That's I guess how you learn, by trying to
> teach.
>
> Linda
>
>
>
>
>
>
>
>
>
> -----Original Message-----
> From: programming-boun...@jsoftware.com
> [mailto:programming-boun...@jsoftware.com] On Behalf Of Kip Murray
> Sent: Friday, December 16, 2011 1:53 PM
> To: Programming forum
> Subject: Re: [Jprogramming] How #: should have been designed
>
> 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: programming-boun...@jsoftware.com
>> [mailto:programming-boun...@jsoftware.com] 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
>
> ----------------------------------------------------------------------
> 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

Reply via email to