home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sys / amiga / programm / 17504 < prev    next >
Encoding:
Internet Message Format  |  1992-12-20  |  2.8 KB

  1. Path: sparky!uunet!ogicse!das-news.harvard.edu!cantaloupe.srv.cs.cmu.edu!crabapple.srv.cs.cmu.edu!andrew.cmu.edu!<UNAUTHENTICATED>+
  2. From: mjw+@cs.cmu.edu (Michael Witbrock)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: alleged Bug in SAS/C V6.1? IS NOT A BUG, but...
  5. Message-ID: <MfAFgcS00hsBMfL35d@cs.cmu.edu>
  6. Date: 17 Dec 92 14:28:24 GMT
  7. Article-I.D.: cs.MfAFgcS00hsBMfL35d
  8. References: <S37732V.92Dec11104414@lk-hp-12.hut.fi> <Bz3nvr.82D@unx.sas.com> <8f=bT9K00hsBMrjYoD@cs.cmu.edu>
  9.     <BzCv72.MBF@unx.sas.com>
  10. Distribution: comp
  11. Organization: Carnegie Mellon, Pittsburgh, PA
  12. Lines: 49
  13. In-Reply-To: <BzCv72.MBF@unx.sas.com>
  14.  
  15. Excerpts from netnews.comp.sys.amiga.programmer: 16-Dec-92 Re: Bug in
  16. SAS/C V6.1? Doug Walker@twix.unx.sas (1890)
  17.  
  18. > |> I'd tend to agree that this optimisation (~0 == -1) is producing the
  19. > |> wrong results, and if it can't stop making the warning, it should
  20. > |> evaluate the ~ at runtime.
  21.  
  22. > The implementation is free to determine value ~0 has, but the standard
  23. > mandates that the TYPE of the value is definately signed int.  On any 
  24. > two's complement machine, ~0 is going to be -1, and that's what it is 
  25. > on our implementation.  The real problem here is not that the value is
  26. > -1, but rather that you have an overflow:  you are assigning
  27. > 0xffffffff to something that can hold at most 0xffff.  You've lost
  28. > the upper four f's.  The fact that the compiler prints the constant
  29. > as -1 is an artifact of the given machine we're running on, but even
  30. > on a one's complement machine there is precision lost here.
  31.  
  32. It doesn't like 
  33. unsigned short x = ~(unsigned short)0;
  34. either.
  35. Shouldn't it?  Maybe ~ can only work on ints.....  He goes and looks at
  36. the ANSI version of 
  37. K&R
  38.  
  39. p. 204 
  40. "A7.4.6 One's Complement Operator
  41.   The operand of the ~ operand must have integral type, and the result
  42. is the one's complement of its operand. The integral promotions are
  43. performed. If the operand is unsigned, the result is computed by
  44. subtracting the value from the largest value of the promoted type [which
  45. seems to be unsigned int, if  I read SS A6.1 correctly]. If the result
  46. is signed, the result is computed by converting the promoted operand to
  47. the corresponding unsigned type, applying ~, and converting back to the
  48. signed type. The type of the result is the type of the promoted operand."
  49.  
  50. OK, so If I understand this right,  
  51. ~0  == ~ [signed] 0 == [signed]  ~ [unsigned int]0 == [signed int] -1   
  52. always. AND the score is SAS/C 1,  detractors, 0.
  53.  
  54. However.
  55. ~(unsigned short int)0 [the new case that I'm annoyingly introducing], 
  56. is  [unsigned int] 1111 1111 1111 1111B   i.e. a large unsigned number!
  57. (but still an int, which won't fit into an unsigned short) and CAN'T be
  58. written as -1
  59. which the compiler does in its warning. 
  60.  
  61. So the score of now, I think,  SAS/C 1, detractors [well, not really, but...] 1
  62.  
  63. michael
  64.