🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #557

Re: subtraction between unsigned ints

#557
From: Mark Brader
Date: Sat, 09 Sep 1989 01:41
32 lines
1728 bytes
> >	unsigned int	u, v;
> >	long int	x;
> >	u=3; v=5;
> >	x=u-v;

> If long int is the same size as unsigned int, then the big number that
> results from subtraction will become signed.  On a two's-complement
> machine, yes the result will be -2.

No, it will be "implementation-defined behavior"; see the last part of
section 3.2.1.2.  (I assume that "corresponding signed integer" there is
meant to include other signed integral types of the same size as the
corresponding signed integer!  In any event, overflow situations involving
signed integer types are generally implementation-defined behavior.)

On a two's-complement machine where signed integers give modular arithmetic
with overflows ignored, i.e. the common case, yes the result is -2.
(Yes, I mean modular.  You can do arithmetic modulo 8 using the numbers
-4, -3, ..., +3 just as well as using 0, 1, ... 7, and similarly for any
other modulus, such as the 2-to-the-wordsize applicable here.)

> If long int is longer than unsigned int, then the big number that
> results from subtraction will remain a big number.

Correct.

-- 
Mark Brader			"A hundred billion is *not* infinite
SoftQuad Inc., Toronto		 and it's getting less infinite all the time!"
utzoo!sq!msb, msb@sq.com		-- Isaac Asimov, "The Last Question"

This article is in the public domain.
Technical Details
Message-ID: <1989Sep9.014154.10118@sq.sq.com>
Path: arc1-ca.pugleaf.net!utzoo!attcan!lsuc!sq!msb
References: <KURO.89Sep5111940@shochu.Sun.Com> <10808@riks.csl.sony.co.jp>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!lsuc!sq!msb
From: msb@sq.sq.com (Mark Brader)
Newsgroups: comp.std.c
Subject: Re: subtraction between unsigned ints
Message-ID: <1989Sep9.014154.10118@sq.sq.com>
Date: 9 Sep 89 01:41:54 GMT
References: <KURO.89Sep5111940@shochu.Sun.Com> <10808@riks.csl.sony.co.jp>
Reply-To: msb@sq.com (Mark Brader)
Organization: SoftQuad Inc., Toronto
Lines: 32