#include <stdio.h>
int main (void)
{
signed short x = -4;
unsigned short y = 3;
if (x > y)
puts ("x is greater than y"); // This is the result.
else
puts ("y is greater than x"); // This should show.
return 0;
}
I know this would contradict ANSI C and possibly ANSI C++. but does anyone know if it's possible to do the comparison without either variable being casted to something else? I've tried using the disable warning pragma command, but that did not help.
---
psilord.com
Byte Size Games
Edited by - Densun on 12/30/00 9:58:27 AM
Comparing signed and unsigned variables
I recently discovered about the castings done to expressions that involve comparing signed and unsigned variables. The signed will get converted to unsigned, possibly resulting in a wrong answer (which would explain troubles I've had before).
Maybe not pretty, but maybe this would work:
/* MACRO for determining if a signed or unsigned value x is greater than a signed or unsigned value y.
The first macro line handles the case of y being less than 0
and x being greater than or equal to 0.
The second macro line handles the case of x being less than 0
and y being greater or equal to 0.
The last macro line handles the case of x and y both being
unsigned or signed.
*/
#define GTHAN(x,y) ( (x) >= 0 && (y) < 0 ? 1 : \ // else
( (x) < 0 && (y) >= 0 ? 0 : \ // else
(x) > (y) ))
// example
if (GTHAN (x, y))
...
I havn't tried this, and there may be better solutions with less CPU cycles and more readable code. Then again, maybe this is what you need to do. I think this should work.
Edited by - bishop_pass on December 30, 2000 12:04:44 PM
/* MACRO for determining if a signed or unsigned value x is greater than a signed or unsigned value y.
The first macro line handles the case of y being less than 0
and x being greater than or equal to 0.
The second macro line handles the case of x being less than 0
and y being greater or equal to 0.
The last macro line handles the case of x and y both being
unsigned or signed.
*/
#define GTHAN(x,y) ( (x) >= 0 && (y) < 0 ? 1 : \ // else
( (x) < 0 && (y) >= 0 ? 0 : \ // else
(x) > (y) ))
// example
if (GTHAN (x, y))
...
I havn't tried this, and there may be better solutions with less CPU cycles and more readable code. Then again, maybe this is what you need to do. I think this should work.
Edited by - bishop_pass on December 30, 2000 12:04:44 PM
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
What compiler are you using? Your original code works the way it should with my compiler. I use VC++ 4.
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
I have VC++ 5. What compiler options do you have? I used Warning level 3 but I didn''t try using the /Za compiler option (use only ANSI C) because it came up with some error I was too tired to fix.
I think I''d rather convert the unsigned to a signed than use the macro.
---
psilord.com
Byte Size Games
I think I''d rather convert the unsigned to a signed than use the macro.
---
psilord.com
Byte Size Games
I''ve just finnished a course on Computer Systems Fundamentals where 99% of the time they were trying to explain how computers store numbers.
the if statement that you are evaluating is not realy converted to an unsigned or signed value it is just compared :
if( -4 > 3 ) is realy if( 65532 > 3 ) so the correct information is displayed. To get the correct answer you will have to override the if statement by doing this:
if( (signed) x > (signed) y )
I put the extra sign in there so that the code is easier to understand at a later date, and to force both variables to use the correct signed values.
If i have got some of this wrong then please blame my drunken tutor for any and all errors.
When I find my code in tons of trouble,
Friends and colleages come to me,
Speaking words of wisdom:
"Write in C."
the if statement that you are evaluating is not realy converted to an unsigned or signed value it is just compared :
if( -4 > 3 ) is realy if( 65532 > 3 ) so the correct information is displayed. To get the correct answer you will have to override the if statement by doing this:
if( (signed) x > (signed) y )
I put the extra sign in there so that the code is easier to understand at a later date, and to force both variables to use the correct signed values.
If i have got some of this wrong then please blame my drunken tutor for any and all errors.
When I find my code in tons of trouble,
Friends and colleages come to me,
Speaking words of wisdom:
"Write in C."
When I find my code in tons of trouble,Friends and colleages come to me,Speaking words of wisdom:"Write in C."My Web Site
Try this instead:
I''m not 100% sure, but I believe that should work even if your unsigned value is greater than the max signed value. (or you could cast x to unsigned for the x > y comparison).
#include <stdio.h>int main (void){ signed short x = -4; unsigned short y = 3; if (x < 0) puts ("y is greater than x"); else if (x > y) puts ("x is greater than y); else puts ("y is greater than x); return 0;}
I''m not 100% sure, but I believe that should work even if your unsigned value is greater than the max signed value. (or you could cast x to unsigned for the x > y comparison).
if you''re using shorts on a 32-bit system, cast them to signed longs for your comparison. A signed long can hold all the values that both a signed short and an unsigned short can, so the comparison will always be correct, without possibly breaking any of the values as you would if you just cast a large unsigned short to a signed short for example.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement