kuro@shochu.Sun.Com (Teruhiko Kurosaka - Sun Intercon) (09/06/89)
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? I am talking about this situation like: unsigned int u, v; long int x; u=3; v=5; x=u-v; Is x guranteed to be -2? ------- T. Kurosaka ("Kuro") --- Sun Microsystems, Intercontinental Operation Internet: kuro@Corp.Sun.Com Voice:+1(415)496 6121 Fax: +1(415)858 0284 US Mail: Mail Stop A6-18, 1870 Embarcadero Rd., Palo Alto, CA 94303, USA -- ------- T. Kurosaka ("Kuro") --- Sun Microsystems, Intercontinental Operation Internet: kuro@Corp.Sun.Com Voice:+1(415)496 6121 Fax: +1(415)858 0284 US Mail: Mail Stop A6-18, 1870 Embarcadero Rd., Palo Alto, CA 94303, USA
diamond@csl.sony.co.jp (Norman Diamond) (09/06/89)
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? 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@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.
gwyn@smoke.BRL.MIL (Doug Gwyn) (09/06/89)
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.
henry@utzoo.uucp (Henry Spencer) (09/06/89)
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? Then >what would be the result of subtraction of a number from a >smaller number? ... Unsigned - unsigned gives unsigned. Unsigned arithmetic is done modulo the type size, roughly speaking, so carries and borrows off the top end are ignored. The value of "(unsigned)3 - (unsigned)5" depends on how big the unsigned type is, but on a 16-bit machine it's probably (unsigned)65534. -- V7 /bin/mail source: 554 lines.| Henry Spencer at U of Toronto Zoology 1989 X.400 specs: 2200+ pages. | uunet!attcan!utzoo!henry henry@zoo.toronto.edu
walter@hpclwjm.HP.COM (Walter Murray) (09/08/89)
Doug Gwyn writes: >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. But note that the usual arithmetic conversions are still performed. So if s1 and s2 have type short int, for example, the expression (s1+s2) has type int, not short int. Walter Murray -------------
walter@hpclwjm.HP.COM (Walter Murray) (09/09/89)
Norman Diamond 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 think it was required by K&R. See Appendix A, section 6.5, in the first edition (page 184). Walter Murray --
gwyn@smoke.BRL.MIL (Doug Gwyn) (09/09/89)
In article <12570023@hpclwjm.HP.COM> walter@hpclwjm.HP.COM (Walter Murray) writes: -Doug Gwyn writes: ->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. -But note that the usual arithmetic conversions are still performed. -So if s1 and s2 have type short int, for example, the expression -(s1+s2) has type int, not short int. Yes, I was trying to put it succinctly. What I described was strictly accurate only for arithmetic types that don't undergo the usual arithmetic conversions when combined with other operands of their own type. Unsigned int is one of those.
msb@sq.sq.com (Mark Brader) (09/09/89)
> > 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.
diamond@csl.sony.co.jp (Norman Diamond) (09/13/89)
Someone asked: >>>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? I answered: >>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. In article <12570024@hpclwjm.HP.COM> walter@hpclwjm.HP.COM (Walter Murray) writes: >I think it was required by K&R. See Appendix A, section 6.5, in the >first edition (page 184). That section specifies conversion from [signed] int to unsigned int. It does not specify the result of subtraction. Hmm, this raises a new question. Someone who had the good fortune to receive service from Global Engineering Documents, please kindly answer: Does the standard specify the same conversion as K&R-1 did? Or is this another quiet change? On a 1's complement machine, K&R-1 required more computation than just a move instruction. Do the pants impose the same requirement? -- -- Norman Diamond, Sony Corporation (diamond@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.
karl@haddock.ima.isc.com (Karl Heuer) (09/16/89)
In article <10833@riks.csl.sony.co.jp> diamond@ws.sony.junet (Norman Diamond) writes: >Hmm, this raises a new question. [Does pANS agree with K&R that a conversion >from int to unsigned is a nontrivial operation on one's complement machines?] Yes. (Whether existing compilers actually follow that rule is another question, to which I do not have first-hand knowledge.) Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint