[Search for users]
[Overall Top Noters]
[List of all Conferences]
[Download this site]
Title: | DEC C Problem Reporting Forum |
Notice: | Report DEC C++ problems in TURRIS::C_PLUS_PLUS |
Moderator: | CXXC::REPETE TCHEON |
|
Created: | Fri Nov 13 1992 |
Last Modified: | Fri Jun 06 1997 |
Last Successful Update: | Fri Jun 06 1997 |
Number of topics: | 1299 |
Total number of notes: | 6249 |
1288.0. "Incomplete sign extension when returning result..." by PEACHS::DALEY ('Course it don't come with @!# wafers!) Wed Apr 23 1997 17:55
Hi,
I *think* this is a bug. Need confirmation, explanation.
Sign extension doesn't seem to work in the code below, which
should return signed long, but seems to return a signed in
and stuffs it in the first 32bits of a long.
The following code returns a long that's a positive value on
an alpha...mainly because it does sign extension out 32 bits
(as if it were an int), but not 64 bits for the long.
Other vendors machines I've tested with show long as a signed
negative value, but their ints are the same as their longs, so
maybe (I thought) they're just lucky. They do sign extension
from short int to int, and so to we, but not to our long, which
is 8 bytes, compared to everybody else's 4 bytes.
Comments? Here's the code, below.
Regards,
John
---------------------------------------------------------
#include <stdio.h>
long foo();
long l;
int i;
main() {
l = foo();
printf("l as long = %ld\n", l);
printf("l as int = %d\n", l);
printf("sizeof long l = %d\n", sizeof(l));
printf("sizeof int i = %d\n", sizeof(i));
}
long foo() {
unsigned int x = 1;
unsigned int y = 26;
return(x - y);
}
T.R | Title | User | Personal Name | Date | Lines |
---|
1288.1 | Expected | WIBBIN::NOYCE | Pulling weeds, pickin' stones | Wed Apr 23 1997 18:05 | 13 |
| > unsigned int x = 1;
> unsigned int y = 26;
>
> return(x - y);
This computes (x-y) using "unsigned int" arithmetic, getting a large
positive value (unsigned arithmetic "wraps" on overflow, as in this case).
Then it converts that large positive value to a long int (getting the
same value, of course) and returns it. This behavior is required by the
C language.
You probably want to write
return ( (long int)x - (long int)y );
|
1288.2 | Thanks. | PEACHS::DALEY | 'Course it don't come with @!# wafers! | Wed Apr 23 1997 18:51 | 0
|