 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
MiniDisc_2k2 Guest
|
Posted: Thu Jul 10, 2003 8:34 pm Post subject: Unsigned/Signed Mismatch |
|
|
Okay, here's a question about the standard. What does it say about
unsigned/signed mismatches in a comparison statement:
char a = 3;
unsigned char b = 255;
if (a<b)
Now what's the real answer here? If a is converted to unsigned, then b>a.
But, if b is converted to signed,then a>b. What's the correct coversion
(what is the compiler supposed to do?)
--
MiniDisc_2k2
To reply, replace nospam.com with cox dot net.
---
[ 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 ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Thu Jul 10, 2003 10:49 pm Post subject: Re: Unsigned/Signed Mismatch |
|
|
In article <%jjPa.31$zd4.2@lakeread02>, MiniDisc_2k2
<MattDelB (AT) nospam (DOT) com> writes
| Quote: | Okay, here's a question about the standard. What does it say about
unsigned/signed mismatches in a comparison statement:
char a = 3;
unsigned char b = 255;
if (a<b)
Now what's the real answer here? If a is converted to unsigned, then b>a.
But, if b is converted to signed,then a>b. What's the correct coversion
(what is the compiler supposed to do?)
|
No, the normal integral promotions are applied first Which only becomes
mildly interesting on systems where all integral types are the same
size.
---
[ 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 ]
|
|
| Back to top |
|
 |
MiniDisc_2k2 Guest
|
Posted: Thu Jul 10, 2003 11:10 pm Post subject: Re: Unsigned/Signed Mismatch |
|
|
"Ioannis Vranos" <ivr (AT) nothis (DOT) emails.ru> wrote
| Quote: | ""MiniDisc_2k2"" <MattDelB (AT) nospam (DOT) com> wrote in message
news:%jjPa.31$zd4.2 (AT) lakeread02 (DOT) ..
Okay, here's a question about the standard. What does it say about
unsigned/signed mismatches in a comparison statement:
char a = 3;
unsigned char b = 255;
if (a<b)
Now what's the real answer here? If a is converted to unsigned, then
b>a.
But, if b is converted to signed,then a>b. What's the correct coversion
(what is the compiler supposed to do?)
At first keep in mind that char (differently from other "plain" integer
types) can be either signed or unsigned. If you want one of the two in
specific, you have to declare it explicitly (e.g. signed char a=3).
Assuming you wish your above code to be:
signed char a = 3;
unsigned char b = 255;
if (a
Both unsigned char and signed char get converted to int during the
expression evaluation.
So in summary, a either signed char either unsigned gets converted to int.
|
Well actually I was using chars only to save my mind from calculating it all
out. Let's try again.
I also intended this code to be used on a two's complement machine.
Use this:
signed int a = 3;
unsigned int b = -1; /* thus being the highest number represented by
unsigned int*/
if (b
Now. If b were converted to a signed integer, b
a were converted to an unsigned integer, we would get 65535 < 3 == false (in
16-bit environment). Which representation does it follow. Does it proceed
like the char conversion, where it is converted to a signed int? Is this at
all standardized, or does the compiler get to pick?
--
MiniDisc_2k2
---
[ 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 ]
|
|
| Back to top |
|
 |
Andy Sawyer Guest
|
Posted: Thu Jul 10, 2003 11:48 pm Post subject: Re: Unsigned/Signed Mismatch |
|
|
In article <%jjPa.31$zd4.2@lakeread02>,
on Thu, 10 Jul 2003 20:34:29 +0000 (UTC),
[email]MattDelB (AT) nospam (DOT) com[/email] ("MiniDisc_2k2") wrote:
| Quote: | Okay, here's a question about the standard. What does it say about
unsigned/signed mismatches in a comparison statement:
char a = 3;
unsigned char b = 255;
if (a<b)
Now what's the real answer here? If a is converted to unsigned, then b>a.
But, if b is converted to signed,then a>b
What's the correct coversion
(what is the compiler supposed to do?)
|
5.9p2 tells us that the usual arithmetic conversions (5p9) are performed.
4.5p1 tells us that 'a' is promoted to signed int,
It also tells us that b is promoted to either signed int or unsigned
int, depending on the implementation, specifically:
,----
| Quote: | An rvalue of type char, signed char, unsigned char, short int, or
unsigned short int can be converted to an rvalue of type int if int
can represent all the values of the source type; otherwise, the source
rvalue can be converted to an rvalue of type unsigned int.
`---- |
Either way, it will still have the value 255 (I assume from your magic
number of 255, you're thinking of a platform with 8-bit characters and
you're concerned about sign-extention? Not a problem here :)
If b has been promoted to signed int (such as in an 8-bit char
environment), then the comparison is performed with signed integers, so
(a
If b has been promoted to unsigned int, a is then also converted to
unsigned int (5p9) and the comparison is performed using unsigned
integers, and again (a
In this last case (b promoted to unsigned, a converted to unsigned),
then the result is highly dependant on the actual value of a, as 4.7p3
says:
,----
| Quote: | If the destination type is signed, the value is unchanged if it can be
represented in the destination type (and bitfield width); otherwise,
the value is implementation-defined.
`---- |
Since, in your example, a == 3, then it can comfortable fit inside an
unsigned int and hence is unchanged.
At least, that's the way _I_ read the standard :)
Regards,
Andy S.
--
"Light thinks it travels faster than anything but it is wrong. No matter
how fast light travels it finds the darkness has always got there first,
and is waiting for it." -- Terry Pratchett, Reaper Man
---
[ 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 ]
|
|
| Back to top |
|
 |
Andy Sawyer Guest
|
Posted: Fri Jul 11, 2003 1:39 am Post subject: Re: Unsigned/Signed Mismatch |
|
|
In article <3nmPa.50$zd4.42@lakeread02>,
on Thu, 10 Jul 2003 23:10:49 +0000 (UTC),
[email]MattDelB (AT) nospam (DOT) com[/email] ("MiniDisc_2k2") wrote:
| Quote: | Well actually I was using chars only to save my mind from calculating it all
out. Let's try again.
I also intended this code to be used on a two's complement machine.
Use this:
signed int a = 3;
unsigned int b = -1; /* thus being the highest number represented by
unsigned int*/
|
No, it's a signed integer converted to an unsigned integer. And it's a
value that the unsigned target cannot represent, so the value of b is
actually implementation-defined.
If you *actually* want the highest value represented by an unsigned
integer, then write::
unsinged int b = std::numeric_limits<unsigned int>::max();
| Quote: | if (b
Now. If b were converted to a signed integer
|
Which it won't be.
| Quote: | b
a were converted to an unsigned integer
|
It will be.
| Quote: | we would get 65535 < 3 == false (in
16-bit environment). Which representation does it follow. Does it proceed
like the char conversion, where it is converted to a signed int? Is this at
all standardized, or does the compiler get to pick?
|
It's standardized in detail, see:
4.5 Integral Promotions
4.7 Integral Conversions
5p9 "the usual suspects^H^H^H^H^H^H^H^H arithmetic conversions":)
What the compiler *can* pick is the result of the conversion from signed
to unigned types if the value of the signed type cannot be represented
in the unsigned type (see 4.7p3)
Regards,
Andy S
--
"Light thinks it travels faster than anything but it is wrong. No matter
how fast light travels it finds the darkness has always got there first,
and is waiting for it." -- Terry Pratchett, Reaper Man
---
[ 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 ]
|
|
| Back to top |
|
 |
Andy Sawyer Guest
|
Posted: Fri Jul 11, 2003 4:06 pm Post subject: Re: Unsigned/Signed Mismatch |
|
|
In article <3f0dd160$0$245$4d4eb98e (AT) read (DOT) news.gr.uu.net>,
on Thu, 10 Jul 2003 21:55:32 +0000 (UTC),
[email]ivr (AT) nothis (DOT) emails.ru[/email] (Ioannis Vranos) wrote:
| Quote: | ""MiniDisc_2k2"" <MattDelB (AT) nospam (DOT) com> wrote in message
news:%jjPa.31$zd4.2 (AT) lakeread02 (DOT) ..
Okay, here's a question about the standard. What does it say about
unsigned/signed mismatches in a comparison statement:
char a = 3;
unsigned char b = 255;
if (a<b)
Now what's the real answer here? If a is converted to unsigned, then b>a.
But, if b is converted to signed,then a>b. What's the correct coversion
(what is the compiler supposed to do?)
At first keep in mind that char (differently from other "plain" integer
types) can be either signed or unsigned. If you want one of the two in
specific, you have to declare it explicitly (e.g. signed char a=3).
|
Remember that char, signed char and unsigned char are three disticnt
types. FWIW, if I'm using the value in an arithmetic context, then I
always write either signed char or unsigned char. If I'm using it to
represent text elements, then I write char. And I probably wouldn't
write:
char a = 3;
I'd write
char a = ' 03';
or, more likely - given my current project:
char a = ASCII::ETX;
| Quote: | Assuming you wish your above code to be:
signed char a = 3;
unsigned char b = 255;
if (a
|
I suspect if he'd wished that, he'd have written that.
| Quote: | Both unsigned char and signed char get converted to int during the
expression evaluation.
|
In fact, unsigned char may be promoted to unsigned int (depending on the
environment).
| Quote: | So in summary, a either signed char either unsigned gets converted to
int.
|
I had trouble parsing that - I assume you meant
"So in summary, either a signed char or unsigned char gets converted to
int."
?
If that is what you meant, then it isn't strictly true on all
platforms. Please see my other post for why :)
Regards,
Andy S.
--
"Light thinks it travels faster than anything but it is wrong. No matter
how fast light travels it finds the darkness has always got there first,
and is waiting for it." -- Terry Pratchett, Reaper Man
---
[ 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 ]
|
|
| Back to top |
|
 |
William M. Miller Guest
|
Posted: Fri Jul 11, 2003 5:52 pm Post subject: Re: Unsigned/Signed Mismatch |
|
|
"Andy Sawyer" <andys (AT) despammed (DOT) com> wrote
| Quote: | In article <3nmPa.50$zd4.42@lakeread02>,
on Thu, 10 Jul 2003 23:10:49 +0000 (UTC),
[email]MattDelB (AT) nospam (DOT) com[/email] ("MiniDisc_2k2") wrote:
signed int a = 3;
unsigned int b = -1; /* thus being the highest number represented by
unsigned int*/
No, it's a signed integer converted to an unsigned integer. And it's a
value that the unsigned target cannot represent, so the value of b is
actually implementation-defined.
....
What the compiler *can* pick is the result of the conversion from signed
to unigned types if the value of the signed type cannot be represented
in the unsigned type (see 4.7p3)
|
Wrong paragraph. 4.7p3 deals with conversion to signed types
("if the destination type is signed..."), and the result is
implementation-defined, as you say. The description of
conversion to an unsigned type is in 4.7p2, and the result is
defined by the Standard, not by the implementation:
If the designation type is unsigned, the resulting value is
the least unsigned integer congruent to the source integer
(modulo 2**n where n is the number of bits used to represent
the unsigned type). [Note: In a two's complement
representation, this conversion is conceptual and there is
no change in the bit pattern (if there is no truncation). ]
-- William M. Miller
---
[ 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 ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sat Jul 12, 2003 12:12 am Post subject: Re: Unsigned/Signed Mismatch |
|
|
""MiniDisc_2k2"" <MattDelB (AT) nospam (DOT) com> wrote
| Quote: | Okay, here's a question about the standard. What does it say about
unsigned/signed mismatches in a comparison statement:
char a = 3;
unsigned char b = 255;
if (a<b)
Now what's the real answer here? If a is converted to unsigned, then b>a.
But, if b is converted to signed,then a>b. What's the correct coversion
(what is the compiler supposed to do?)
|
The usual aritmentic conersions are performd (5.1/9) which means the
integral promotions (4.5) are performed which means both a and b are
converted to int values of 3 and 255 respectively. The value of a
therefore is true.
---
[ 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 ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Sat Jul 12, 2003 12:15 am Post subject: Re: Unsigned/Signed Mismatch |
|
|
In article <llv6szff.fsf (AT) evo6 (DOT) com>, Andy Sawyer wrote:
| Quote: | In article <3nmPa.50$zd4.42@lakeread02>,
on Thu, 10 Jul 2003 23:10:49 +0000 (UTC),
[email]MattDelB (AT) nospam (DOT) com[/email] ("MiniDisc_2k2") wrote:
Well actually I was using chars only to save my mind from calculating it
all out. Let's try again.
I also intended this code to be used on a two's complement machine.
Use this:
signed int a = 3;
unsigned int b = -1; /* thus being the highest number represented by
unsigned int*/
No, it's a signed integer converted to an unsigned integer. And it's a
value that the unsigned target cannot represent, so the value of b is
actually implementation-defined.
snip |
No, this is well-defined (standard 4.7p2). It's the reverse that 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 ]
|
|
| Back to top |
|
 |
Ioannis Vranos Guest
|
Posted: Sat Jul 12, 2003 12:16 am Post subject: Re: Unsigned/Signed Mismatch |
|
|
"Andy Sawyer" <andys (AT) despammed (DOT) com> wrote
| Quote: |
Remember that char, signed char and unsigned char are three disticnt
types.
|
char is implemented either as signed char or as unsigned char.
FWIW, if I'm using the value in an arithmetic context, then I
| Quote: | always write either signed char or unsigned char. If I'm using it to
represent text elements, then I write char. And I probably wouldn't
write:
char a = 3;
I'd write
char a = ' 03';
|
It's a personal issue. I am thinking in integral form, so i would write 3 if
i had not a specific character in mind.
| Quote: | In fact, unsigned char may be promoted to unsigned int (depending on the
environment).
|
If the involved values fit in int, they get promoted to int. It's in the
standard.
| Quote: |
So in summary, a either signed char either unsigned gets converted to
int.
I had trouble parsing that - I assume you meant
"So in summary, either a signed char or unsigned char gets converted to
int."
?
|
"So in summary the variable a, either it is signed char or unsigned char
gets converted to int (in real world scenarios where sizeof(int)>1)".
--
Ioannis
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
---
[ 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 ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Sun Jul 13, 2003 3:58 pm Post subject: Re: Unsigned/Signed Mismatch |
|
|
[email]ivr (AT) nothis (DOT) emails.ru[/email] ("Ioannis Vranos") writes:
| Quote: | ""MiniDisc_2k2"" <MattDelB (AT) nospam (DOT) com> wrote in message
news:3nmPa.50$zd4.42 (AT) lakeread02 (DOT) ..
Well actually I was using chars only to save my mind from
calculating it all out. Let's try again.
I also intended this code to be used on a two's complement
machine.
|
Which doesn't change anything here.
| Quote: | Use this:
signed int a = 3;
unsigned int b = -1; /* thus being the highest number represented by
unsigned int*/
if (b
Now. If b were converted to a signed integer, b
== true. If a were converted to an unsigned integer, we would
get 65535 < 3 == false (in 16-bit environment). Which
representation does it follow. Does it proceed like the char
conversion, where it is converted to a signed int? Is this at
all standardized, or does the compiler get to pick?
The C++ standard says:
[conv.prom] 4.5 Integral promotions
|
Wrong section. There are no promotions here.
| Quote: | 1 An rvalue of type char, signed char, unsigned char, short int,
or unsigned short int can be converted to an rvalue of type int if
int can represent all the values of the source type; otherwise,
the source rvalue can be converted to an rvalue of type unsigned
int.
|
[...]
| Quote: | The (1) is your case.
|
Not at all. That paragraph only applies if the type is char, signed
char, unsigned char, short int or unsigned short int. There are none
of those here.
| Quote: | The answer is that everything is promoted to unsigned int and you
must get a compiler warning about comparing signed with unsigned
values.
|
Right answer, wrong reason. See 5/9 -- it gives the complete list of
conversions (call the usual arithmetic conversions) which apply to
binary operators. In particular, the last point "Otherwise, if either
operand is unsigned, the other shall be converted to unsigned."
| Quote: | Try to compile and see.
|
That's not always a valid solution. If he were comparing a long and
an unsigned int, for example, the conversions used will depend on the
implementation.
--
James Kanze mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France +33 1 41 89 80 93
---
[ 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 ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Sun Jul 13, 2003 3:58 pm Post subject: Re: Unsigned/Signed Mismatch |
|
|
[email]francis (AT) robinton (DOT) demon.co.uk[/email] (Francis Glassborow) writes:
| Quote: | In article <%jjPa.31$zd4.2@lakeread02>, MiniDisc_2k2
[email]MattDelB (AT) nospam (DOT) com[/email]> writes
Okay, here's a question about the standard. What does it say about
unsigned/signed mismatches in a comparison statement:
char a = 3;
unsigned char b = 255;
if (a<b)
Now what's the real answer here? If a is converted to unsigned,
then b>a. But, if b is converted to signed,then a>b. What's the
correct coversion (what is the compiler supposed to do?)
No, the normal integral promotions are applied first Which only
becomes mildly interesting on systems where all integral types are
the same size.
|
But on such systems, 255 is within the range of char, so the problem
isn't interesting either. (For that matter, as formulated, it isn't
interesting on a system with 9 bit bytes either.)
--
James Kanze mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France +33 1 41 89 80 93
---
[ 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 ]
|
|
| Back to top |
|
 |
Andy Sawyer Guest
|
Posted: Mon Jul 14, 2003 5:26 pm Post subject: Re: Unsigned/Signed Mismatch |
|
|
In article <3f0ef04e$0$242$4d4eb98e (AT) read (DOT) news.gr.uu.net>,
on Sat, 12 Jul 2003 00:16:53 +0000 (UTC),
[email]ivr (AT) nothis (DOT) emails.ru[/email] ("Ioannis Vranos") wrote:
| Quote: | "Andy Sawyer" <andys (AT) despammed (DOT) com> wrote in message
news:ptkiszvi.fsf (AT) evo6 (DOT) com...
Remember that char, signed char and unsigned char are three disticnt
types.
char is implemented either as signed char or as unsigned char.
|
Please cite your reference. I suggest you read 3.9.1p1, which says in part:
,----
| Quote: | Plain char, signed char, and unsigned char are three distinct types.
`---- |
It's in the standard (almost word-for-word - I omitted "plain").
| Quote: | In fact, unsigned char may be promoted to unsigned int (depending on the
environment).
If the involved values fit in int, they get promoted to int. It's in the
standard.
|
Again, cite your reference. I suggest you read 4.5p1, which says:
,----
| Quote: | An rvalue of type char, signed char, unsigned char, short int, or
unsigned short int can be converted to an rvalue of type int if int can
represent all the values of the source type; otherwise, the source
rvalue can be converted to an rvalue of type unsigned int.
`---- |
It's in the standard.
Note the "if int can represent *ALL* values of the source type". (my
emphasis). I suspect you're thinking of integral _conversions_, which
(in this context) happen after promotions - see 5p9. It's in the standard.
| Quote: | So in summary, a either signed char either unsigned gets converted to
int.
I had trouble parsing that - I assume you meant
"So in summary, either a signed char or unsigned char gets converted to
int."
?
"So in summary the variable a, either it is signed char or unsigned char
gets converted to int
|
Not necessarily - it's in the standard.
| Quote: | (in real world scenarios where sizeof(int)>1)".
|
There are real world scenarios where sizeof(int)==sizeof(char). Neither
your being unfamilair with them, nor the fact that they are less common
than those where sizeof(int)>sizeof(char) does not in any way disprove
their existence.
Regards,
Andy S.
--
"Light thinks it travels faster than anything but it is wrong. No matter
how fast light travels it finds the darkness has always got there first,
and is waiting for it." -- Terry Pratchett, Reaper Man
---
[ 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 ]
|
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|