Let us add 1 1 0 and 0 1 0 in a simple-minded way working from right to
left, and writing carries above the addition:
1 1 0 <-- these are carries
1 1 0
0 1 0
-----
1 0 0 0
If we keep only the right three bits in our answer we get 0 0 0 . For
this reason in two's-complement notation 1 1 0 represents _2 -- because
when you "add" it to 0 1 0 you get 0 0 0 (because of keeping only three
bits in the answer). For the same reason 1 1 1 represents _1 in two's
complement notation: when you "add" it to 0 0 1 you get 0 0 0 .
Below hcinv finds the two's-compliment values of the three-bit numbers.
On 12/10/2011 8:23 PM, Linda Alvord wrote:
> I just finished posting my thoughts on this and found your function Henry.
>
> hcinv =: ([: -/ [: #. (,: [: +: 1 {. ]))"1
> d
> 1 1 0
> 1 1 1
> 0 0 0
> 0 0 1
> 0 1 0
>
> hcinv d
> _2 _1 0 1 2
>
> This shouldn't be _2 _1 0 1 2 because d really is 6 7 0 1 2
>
> ]c
> _1 1 0
> 0 0 _1
> 0 0 0
> 0 1 _1
> 0 1 0
>
> 2 2 2#.c
> _2 _1 0 1 2
>
> Base seems happy with negative numbers but #: is not so happy.
>
> Linda
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Kip Murray
> Sent: Saturday, December 10, 2011 7:48 PM
> To: Programming forum
> Subject: Re: [Jprogramming] How #: should have been designed
>
> Cool. I think it is an improvement because it neatly avoids a case
> statement. Now I wonder if we could implement two's-complement addition
> and multiplication with overflow, basing these on bitwise operations
>
> 0 + 0 is 0, 0 + 1 is 1 + 0 is 1, and 1 + 1 is 1 0
>
> 0 * 0 is 0 * 1 is 1 * 0 is 0, and 1*1 is 1
>
> That is, I do not want hc and hcinv to be used except to produce data
> and check answers, and I want n-bit two's-complement answers for n-bit
> two's-complement data so some of them will be wrong because of overflow.
>
> Here for Linda is Henry's hcinv expressed without conjunctions other
> than " .
>
> hcinv =: ([: -/ [: #. (,: [: +: 1 {. ]))"1
>
> On 12/10/2011 10:00 AM, Henry Rich wrote:
>> Same idea, better implementation
>>
>> hcinv =. -/@:#.@:(,: +:@(1&{.))"1
>>
>> Henry Rich
>>
>> On 12/10/2011 10:48 AM, Henry Rich wrote:
>>> Maybe not an improvement, but this is how I would have done back in my
>>> hardware-design days:
>>>
>>> hcinv =. -~/@:#.@:(~:/\)@:((,1)&,:)"1
>>>
>>> Quite a bit more elegant in wires than in J!
>>>
>>> Henry Rich
>>>
>>> On 12/10/2011 1:01 AM, Kip Murray wrote:
>>>> Now we need an inverse for Raul's improved #: ("hash colon")
>>>>
>>>> hc NB. Raul's improved #:
>>>> {.@#:@(,: (2 * |))
>>>>
>>>> hc i: 2
>>>> 1 1 0
>>>> 1 1 1
>>>> 0 0 0
>>>> 0 0 1
>>>> 0 1 0
>>>>
>>>> hcinv NB. improvement sought
>>>> 2&#.`(2&#.@}. - 2 ^<:@#)@.{."1
>>>>
>>>> hcinv hc i: 2
>>>> _2 _1 0 1 2
>>>>
>>>> On 12/8/2011 2:41 PM, Raul Miller wrote:
>>>> ...
>>>> But this would work just as well, as a model:
>>>>
>>>> {.@#:@(,: 2 * |)i:2
>>>> 1 1 0
>>>> 1 1 1
>>>> 0 0 0
>>>> 0 0 1
>>>> 0 1 0
>>>> ----------------------------------------------------------------------
>>>> 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