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