Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 15 Nov 2000 19:24:43 -0800 (PST)
From:      John Polstra <jdp@polstra.com>
To:        net@freebsd.org
Cc:        louie@TransSys.COM
Subject:   Re: libalias: Incremental Update of Internet Checksum 
Message-ID:  <200011160324.eAG3Ohi47268@vashon.polstra.com>
In-Reply-To: <200011152043.eAFKhMG68318@whizzo.transsys.com>
References:  <Pine.BSF.4.21.0011130015100.50906-100000@carcassonne.scientech.com> <200011151548.eAFFmJG66031@whizzo.transsys.com> <20001115180842.A11913@sunbay.com> <200011152043.eAFKhMG68318@whizzo.transsys.com>

next in thread | previous in thread | raw e-mail | index | archive | help
In article <200011152043.eAFKhMG68318@whizzo.transsys.com>,
Louis A. Mamakos <louie@TransSys.COM> wrote:
> 
> > You mentioned the books...  Can you citate from one of these books that
> > states that 0xFFFF + 0x0001 in one's complement arithmetic is 0x0000
> > rather than 0xFFFF?
> 
> The correct result is 0x0001, since 0xffff is -0, and -0 + 1 == 1.
> 0xFFFE + 0x0001 == 0, because 0xFFFE is -1 and -1 + 1 == 0.

Louie is correct.  (And in case there's any doubt, my HP 16c confirms
it in 16-bit 1's complement mode.)  To add two numbers in 1's
complement you do this:

1. Perform a normal 2's complement add, and keep track of whether
there was a carry out of the high-order bit.

2. If there was a carry, increment (2's complement) the result.

In other words:

    u_int16_t
    add1(u_int16_t a, u_int16_t b)
    {
	u_int32_t sum;

	sum = (u_int32_t)a + (u_int32_t)b;	/* Carry goes to bit 16 */
	sum += sum >> 16;			/* Add in the carry */
	return (u_int16_t)sum;
    }

They used to call this "wrap-around carry".

John Polstra, CDC-1604 survivor


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-net" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200011160324.eAG3Ohi47268>