🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #526

Re: subtraction between unsigned ints

#526
From: Doug Gwyn
Date: Wed, 06 Sep 1989 10:22
23 lines
1374 bytes
In article <KURO.89Sep5111940@shochu.Sun.Com> kuro@shochu.Sun.Com (Teruhiko Kurosaka - Sun Intercon) writes:
>Does pANSI defines the datatype of the result of subtraction
>between unsigned integers?  Would it be also unsigned?

Yes, the type of the result of any arithmetic combination of operands
having a single arithmetic type is the same type as that of the operands.

>Then what would be the result of subtraction of a number from a
>smaller number?

Unsigned arithmetic is always modular arithmetic, that is, it is
performed modulo the word size of the unsigned type involved.

>	unsigned int	u, v;
>	long int	x;
>	u=3; v=5;
>	x=u-v;
>Is x guranteed to be -2?

The answer is no.  For a 16-bit implementation, u-v is 65534 and
has type (unsigned int).  Before the assignment is performed, the
operands of the = operator are promoted to a common type (long int
in this particular scenario), and x receives the value 65534.
Technical Details
Message-ID: <10948@smoke.BRL.MIL>
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!usc!ginosko!aplcen!haven!adm!smoke!gwyn
References: <KURO.89Sep5111940@shochu.Sun.Com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!usc!ginosko!aplcen!haven!adm!smoke!gwyn
From: gwyn@smoke.BRL.MIL (Doug Gwyn)
Newsgroups: comp.std.c
Subject: Re: subtraction between unsigned ints
Message-ID: <10948@smoke.BRL.MIL>
Date: 6 Sep 89 10:22:38 GMT
References: <KURO.89Sep5111940@shochu.Sun.Com>
Reply-To: gwyn@brl.arpa (Doug Gwyn)
Organization: Ballistic Research Lab (BRL), APG, MD.
Lines: 23