subtraction between unsigned ints

Norman Diamond diamond at csl.sony.co.jp
Wed Sep 6 15:54:52 AEST 1989


In article <KURO.89Sep5111940 at shochu.Sun.Com> kuro at 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?  Then
>what would be the result of subtraction of a number from a
>smaller number?

It always has been unsigned, even in K&R days.  pANS says that the
result must wrap around modulo some power of 2; this was not required
by K&R but surely always occured.

>I am talking about this situation like:
>	unsigned int	u, v;
>	long int	x;

Well, that's not quite what you were talking about.  You didn't say,
but sort of implied, that you wanted to know the result when it was
the same size as the operands.  But let's continue.

>	u=3; v=5;
>	x=u-v;
>
>Is x guranteed to be -2?

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.

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

--
-- 
Norman Diamond, Sony Corporation (diamond at ws.sony.junet)
  The above opinions are inherited by your machine's init process (pid 1),
  after being disowned and orphaned.  However, if you see this at Waterloo or
  Anterior, then their administrators must have approved of these opinions.



More information about the Comp.std.c mailing list