RE: Strange conversion to int64

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Tony,

>with the output
4294967232
-64
-64
-64

What output were you expecting?

Is your GCC on your 64-bit platform built such that (say) short int is 32-bit, int is 64-bit, and long is 128-bit?

Keep in mind that integer promotion rules are char/short/int/long based, not int32/int64 based.  And the char/short/int/long is promotion behavior is sensitive (i.e., can change output behavior depending on your platform) to what bit sizes each of those types are.

Sincerely,
--Eljay


________________________________________
From: gcc-help-owner@xxxxxxxxxxx [gcc-help-owner@xxxxxxxxxxx] On Behalf Of Tony Bernardin [sarusama@xxxxxxxxx]
Sent: Wednesday, January 27, 2010 8:10 PM
To: gcc-help@xxxxxxxxxxx
Subject: 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? In
particular I find lines 11 and 18 interesting as the only change
between the type of 'mult' uint32 vs uint64.

cheers,
Tony


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux