Strange conversion to int64
Tony Bernardin
sarusama@gmail.com
Thu Jan 28 02:10:00 GMT 2010
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? In
particular I find lines 11 and 18 interesting as the only change
between the type of 'mult' uint32 vs uint64.
cheers,
Tony
More information about the Gcc-help
mailing list