home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / std / c / 3226 < prev    next >
Encoding:
Internet Message Format  |  1992-12-17  |  2.2 KB

  1. Path: sparky!uunet!mcsun!uknet!gdt!bsmail!smee
  2. From: smee@bristol.ac.uk (Paul Smee)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Is INT_MAX<<1 defined?
  5. Message-ID: <1992Dec17.150312.22536@bristol.ac.uk>
  6. Date: 17 Dec 92 15:03:12 GMT
  7. References: <723345673snx@genesis.demon.co.uk> <71790001@hplvec.LVLD.HP.COM>
  8. Reply-To: P.Smee@bristol.ac.uk (Paul Smee)
  9. Organization: University of Bristol
  10. Lines: 34
  11.  
  12. In article <71790001@hplvec.LVLD.HP.COM> boyne@hplvec.LVLD.HP.COM (Art Boyne) writes:
  13. >In comp.std.c, dik@cwi.nl (Dik T. Winter) writes:
  14. >
  15. >>   What it comes down to is that << is not a mathematical operation, there
  16. >>   is nothing in mathematics that tells us that 16384 << 1 equals 32768.
  17. >>   So what is the meaning of:
  18. >
  19. >I'm not so sure about that.  Conceptually, the common-usage interpretation
  20. >of the operation a << b can be represented mathematically as a*(2^b).
  21.  
  22. Umm, not necessarily on a machine using sign-magnitude representation.
  23. Not on a twos-complement machine if the shift ends up changing the
  24. value of the leftmost bit, even if the register does not overflow.  Not
  25. on a ones-complement machine (are there still any of those?) if a is
  26. negative, when the effective operation becomes a*(2^b)-1.
  27.  
  28. >Given that potential implementation, it is easy to imagine that INT_MAX << 1
  29. >would generate overflow on such a machine.  Now, considering that the ANSI
  30. >standard in general places as few restrictions as possible on implementations
  31. >so as to maximize performance, I'm not sure that there is anything in the
  32. >standard that would judge this implementation as non-conforming.
  33.  
  34. Well, OK, sod the standard for a lark, then. :-)  Let's look at K&R 1:
  35. 'The value of E1<<E2 is E1 (interpreted as a bit pattern) left-shifted
  36. E2 bits.'  I would claim that any 'old' C programmer will expect that
  37. behaviour.  I (and I suspect most 'oldies') would neither buy nor
  38. recommend any C compiler which generated an exception condition for any
  39. 'valid' shift  (i.e. E2 positive, and less than the length of E1).  If
  40. I want multiplication by a power of two with an exception on overflow,
  41. I will WRITE a*(2^b).  Maybe market forces will win.
  42.  
  43. -- 
  44. Paul Smee, Computing Service, University of Bristol, Bristol BS8 1UD, UK
  45.  P.Smee@bristol.ac.uk - ..!uunet!uknet!bsmail!p.smee - Tel +44 272 303132
  46.