[Jprogramming] Two's-complement representation without #: or #.
Raul Miller
rauldmiller at gmail.com
Thu Dec 29 05:06:40 HKT 2011
This is a variation on the ver binary defined a
http://jsoftware.com/pipermail/programming/2011-December/026213.html
effective_magnitude=: (2 * |)^:(-. at -: |)
bit_count=: 2 <.@^. 1>. >./@,@:effective_magnitude
extract_bit=: (%&2 at -&{. , ]) 2&|
binary=: bit_count extract_bit@]^:["1 ,"0
tcr=: 2 | binary
or
tcr=: |@binary
--
Raul
On Wed, Dec 28, 2011 at 3:36 PM, Kip Murray <km at math.uh.edu> wrote:
> Now that we can do binary representation without #: or #. please write a
> two's-complement representation tcr without using #: or #. .
>
> tcr _2
> 1 0
> tcr i: 2
> 1 1 0
> 1 1 1
> 0 0 0
> 0 0 1
> 0 1 0
>
> A straightforward two's-complement value tcv explains the two's
> complement representation:
>
> tcv =: ([: (2 p.~ |.) -@{. , }.)"1 NB. two's-complement value
>
> tcv tcr _2
> _2
> tcv tcr i: 2
> _2 _1 0 1 2
>
> Kip
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list