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