You are right, I had a serious bug here. Here's a corrected version:
twoscomplement=: #:~ 2 #~ 1 + 2 >.@^. 1 + >./@,@:|@(+ 0&>)@:<. -- Raul On Thu, Dec 15, 2011 at 1:52 PM, Kip Murray <k...@math.uh.edu> 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