Topic: What does the C++-Standard say to right shifts for negative values?


Author: gennaro_prota@yahoo.com (Gennaro Prota)
Date: Sat, 19 Oct 2002 19:52:15 +0000 (UTC)
Raw View
On Fri, 18 Oct 2002 14:11:27 CST, "James Kuyper Jr."
<kuyper@wizard.net> wrote:

>However, 6.5.7p4 says:  "If E1 has a signed type and nonnegative value
>.... otherwise, the behavior is undefined."

But the OP was asking about right shift ;-)

Genny.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: gennaro_prota@yahoo.com (Gennaro Prota)
Date: Mon, 21 Oct 2002 17:07:33 +0000 (UTC)
Raw View
On Fri, 18 Oct 2002 11:49:21 +0000 (UTC), mj.heitland@onlinehome.de
(Michael Heitland) wrote:

>In ANSI-C the result of a right shift on negative signed integer
>values is defined to be implementation dependend (A.7.8).
>Example:
>int i = -1;
>i = i >> 1; /* result implementation dependend */
>
>What does ANSI-C++ say?

The same. Also be careful that in both languages the behaviour is
*undefined* if the right operand is negative or >= to the number of
bits of the promoted left operand. For instance, on an implementation
with 16 bit ints and 8 bit chars:

   unsigned char c = 1;
   c >> 15; // ok: yields 0
   c >> 16; // undefined


Genny.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dontspamme@spam.you (Ray Lischner)
Date: Mon, 21 Oct 2002 17:07:56 +0000 (UTC)
Raw View
On Friday 18 October 2002 04:49 am, Michael Heitland wrote:

> In ANSI-C the result of a right shift on negative signed integer
> values is defined to be implementation dependend (A.7.8).
> Example:
> int i = -1;
> i = i >> 1; /* result implementation dependend */
>
> What does ANSI-C++ say?

Implementation-defined.
--
Ray Lischner, author of C++ in a Nutshell (forthcoming)
http://www.tempest-sw.com/cpp/

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kuyper@wizard.net ("James Kuyper Jr.")
Date: Mon, 21 Oct 2002 17:25:28 +0000 (UTC)
Raw View
Gennaro Prota wrote:
> On Fri, 18 Oct 2002 14:11:27 CST, "James Kuyper Jr."
> <kuyper@wizard.net> wrote:
>
>
>>However, 6.5.7p4 says:  "If E1 has a signed type and nonnegative value
>>.... otherwise, the behavior is undefined."
>
>
> But the OP was asking about right shift ;-)

You're correct, of course. I should have referred to 6.5.7p5, which says
"If E1 has a signed type and a negative value, the resulting value is
implementation-defined." which matches the C++ standard exactly.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: mj.heitland@onlinehome.de (Michael Heitland)
Date: Fri, 18 Oct 2002 11:49:21 +0000 (UTC)
Raw View
In ANSI-C the result of a right shift on negative signed integer
values is defined to be implementation dependend (A.7.8).
Example:
int i = -1;
i = i >> 1; /* result implementation dependend */

What does ANSI-C++ say?

Thanks for your help,
Michael

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Ron Natalie" <ron@sensor.com>
Date: Fri, 18 Oct 2002 10:40:33 CST
Raw View
"Michael Heitland" <mj.heitland@onlinehome.de> wrote in message news:713ddd01.0210180018.5aeaec02@posting.google.com...
> In ANSI-C the result of a right shift on negative signed integer
> values is defined to be implementation dependend (A.7.8).
> Example:
> int i = -1;
> i = i >> 1; /* result implementation dependend */
>
> What does ANSI-C++ say?
>

5.8/3

If E1 has a signed type and a negative value, the resulting value

is implementation-defined.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Fri, 18 Oct 2002 14:11:27 CST
Raw View
Michael Heitland wrote:
> In ANSI-C the result of a right shift on negative signed integer
> values is defined to be implementation dependend (A.7.8).

In the current C standard (ISO/IEC 9899:1999) appendix A is devoted to a
summary of the grammar, and A.7.8 doesn't exist.

However, 6.5.7p4 says:  "If E1 has a signed type and nonnegative value
.... otherwise, the behavior is undefined."

> Example:
> int i = -1;
> i = i >> 1; /* result implementation dependend */
> What does ANSI-C++ say?

5.8p3: "If E1 has a signed type and a negative value, the resulting
value is implementation-defined."

Thus, the C++ behavior is much nicer. A C program that does this could,
in principle, short-circuit your refrigerator. For a C++ program, it's
perfectly safe to use such code, and your compiler's documentation must
contain an explanation of what value it returns.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]