From owner-freebsd-net Wed Nov 15 19:24:50 2000 Delivered-To: freebsd-net@freebsd.org Received: from wall.polstra.com (rtrwan160.accessone.com [206.213.115.74]) by hub.freebsd.org (Postfix) with ESMTP id 98A6F37B4C5 for ; Wed, 15 Nov 2000 19:24:47 -0800 (PST) Received: from vashon.polstra.com (vashon.polstra.com [206.213.73.13]) by wall.polstra.com (8.9.3/8.9.3) with ESMTP id TAA22128; Wed, 15 Nov 2000 19:24:44 -0800 (PST) (envelope-from jdp@wall.polstra.com) Received: (from jdp@localhost) by vashon.polstra.com (8.11.0/8.11.0) id eAG3Ohi47268; Wed, 15 Nov 2000 19:24:43 -0800 (PST) (envelope-from jdp) Date: Wed, 15 Nov 2000 19:24:43 -0800 (PST) Message-Id: <200011160324.eAG3Ohi47268@vashon.polstra.com> To: net@freebsd.org From: John Polstra Reply-To: net@freebsd.org Cc: louie@TransSys.COM Subject: Re: libalias: Incremental Update of Internet Checksum In-Reply-To: <200011152043.eAFKhMG68318@whizzo.transsys.com> References: <200011151548.eAFFmJG66031@whizzo.transsys.com> <20001115180842.A11913@sunbay.com> <200011152043.eAFKhMG68318@whizzo.transsys.com> Organization: Polstra & Co., Seattle, WA Sender: owner-freebsd-net@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org In article <200011152043.eAFKhMG68318@whizzo.transsys.com>, Louis A. Mamakos 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