Linda, try this

    NB. 16-"bit" signed binary numbers

    NB. negative is - absolute value

    sbinary =: _1:`1:@.(0 <: ])"0 * (16#2) #: |

    value =: (2 p.~ |.)"1

    NB. 2 7 1 8 p. y  is  +/ 2 7 1 8 * y ^ i. 4

    sbadd =: ([: sbinary +&value)"1

    sbmul =: ([: sbinary *&value)"1

    sbneg =: ([: sbinary [: - value)"1

    ] 'a b' =: sbinary _5 3
0 0 0 0 0 0 0 0 0 0 0 0 0 _1 0 _1
0 0 0 0 0 0 0 0 0 0 0 0 0  0 1  1

    value a ,: b
_5 3

    a (sbadd ,: sbmul) b
0 0 0 0 0 0 0 0 0 0 0 0  0  0 _1  0
0 0 0 0 0 0 0 0 0 0 0 0 _1 _1 _1 _1

    value a (sbadd ,: sbmul) b
_2 _15

    sbneg a ,: b
0 0 0 0 0 0 0 0 0 0 0 0 0 1  0  1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 _1 _1

    value sbneg a ,: b
5 _3

    ]f=: (?6 7$3){_1 0 1
  0 _1 _1  0  1 _1  0
_1  0 _1  1  0  1  1
  0 _1  0  0  1 _1  0
_1  1  1  0 _1 _1  1
  1 _1  0  1  1 _1  0
  0 _1 _1 _1 _1  1 _1

    value f
_46 _69 _30 _21 42 _59

    #. f
_46 _69 _30 _21 42 _59

    sbinary value f
0 0 0 0 0 0 0 0 0  0 _1  0 _1 _1 _1  0
0 0 0 0 0 0 0 0 0 _1  0  0  0 _1  0 _1
0 0 0 0 0 0 0 0 0  0  0 _1 _1 _1 _1  0
0 0 0 0 0 0 0 0 0  0  0 _1  0 _1  0 _1
0 0 0 0 0 0 0 0 0  0  1  0  1  0  1  0
0 0 0 0 0 0 0 0 0  0 _1 _1 _1  0 _1 _1

    value sbinary value f
_46 _69 _30 _21 42 _59

Kip

On 12/16/2011 9:16 PM, Linda Alvord wrote:
...
> 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 you 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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to