This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Strange conversion to int64


Hey, I'm having a little trouble understanding the conversion gcc is
doing in a statement with the following types:

int64 = int32 * uint32

This is running on a 64bit machine with gcc (GCC) 4.4.2 20091027 (Red
Hat 4.4.2-7)

Following simple code snippet:

? 1 #include <stdint.h>
? 2 #include <iostream>
? 3
? 4 using namespace std;
? 5
? 6 int main(int argc, char* argv[])
? 7 {
? 8???? int32_t? neg? = -1;
? 9???? uint32_t mult = 64;
?10
?11???? int64_t res? = neg * mult;
?12???? int32_t res2 = neg * mult;
?13
?14???? cout << res << endl << res2 << endl;
?15
?16???? uint64_t mult64 = 64;
?17
?18???? res? = neg * mult64;
?19???? res2 = neg * mult64;
?20
?21???? cout << res << endl << res2 << endl;
?22
?23???? return 0;
?24 }

with the output

4294967232
-64
-64
-64

When compiled it gives an understandable warning

main.cpp: In function ‘int main(int, char**)’:
main.cpp:19: warning: conversion to ‘int32_t’ from ‘uint64_t’ may
alter its value

still that operation will generate the output I expect.

Is there some strange casting rule that I'm not following properly?

cheers,
Tony


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]