I hate __int64!
-
Hi, I'm having a frustrating little problem with 64-bit integer arithmetic. It goes something like this: 0xba5da800 * 0x38E which should equal: 0x29678EB3000 Unfortunately, the code is a bit like this:
#define unsigned __int64 uint64_t unsigned long myval = 0xba5e3500; uint64_t myfunction(unsigned long val) { return val * 1000; }
This doesn't work, nor does:return ((uint64_t)val * (uint64_t)1000);
orreturn (uint64_t)((uint64_t)val * (uint64_t)1000);
The value I get returned looks like: 0xfffffeefffff0800 (or similar) Now, I've run into problems before with not casting everything into __int64 twice (ok I exadurate slightly) for every calculation - but this is weird. It seems that casting val to uint64_t is turning it into: 0xffffffffba5e3500 Why is this happening?! I truly do hate __int64, any help would be appreciated. thanks, Simon -- Simon Steele Programmers Notepad - http://www.pnotepad.org/ -
Hi, I'm having a frustrating little problem with 64-bit integer arithmetic. It goes something like this: 0xba5da800 * 0x38E which should equal: 0x29678EB3000 Unfortunately, the code is a bit like this:
#define unsigned __int64 uint64_t unsigned long myval = 0xba5e3500; uint64_t myfunction(unsigned long val) { return val * 1000; }
This doesn't work, nor does:return ((uint64_t)val * (uint64_t)1000);
orreturn (uint64_t)((uint64_t)val * (uint64_t)1000);
The value I get returned looks like: 0xfffffeefffff0800 (or similar) Now, I've run into problems before with not casting everything into __int64 twice (ok I exadurate slightly) for every calculation - but this is weird. It seems that casting val to uint64_t is turning it into: 0xffffffffba5e3500 Why is this happening?! I truly do hate __int64, any help would be appreciated. thanks, Simon -- Simon Steele Programmers Notepad - http://www.pnotepad.org/I get the desired results with:
typedef unsigned __int64 uint64_t;
unsigned long myval = 0xba5da800;
uint64_t myfunction(unsigned long val)
{
return (uint64_t) val * (uint64_t) 0x38e;
}void myotherfunction()
{
TRACE("%I64u\n", myfunction(myval));
} -
Hi, I'm having a frustrating little problem with 64-bit integer arithmetic. It goes something like this: 0xba5da800 * 0x38E which should equal: 0x29678EB3000 Unfortunately, the code is a bit like this:
#define unsigned __int64 uint64_t unsigned long myval = 0xba5e3500; uint64_t myfunction(unsigned long val) { return val * 1000; }
This doesn't work, nor does:return ((uint64_t)val * (uint64_t)1000);
orreturn (uint64_t)((uint64_t)val * (uint64_t)1000);
The value I get returned looks like: 0xfffffeefffff0800 (or similar) Now, I've run into problems before with not casting everything into __int64 twice (ok I exadurate slightly) for every calculation - but this is weird. It seems that casting val to uint64_t is turning it into: 0xffffffffba5e3500 Why is this happening?! I truly do hate __int64, any help would be appreciated. thanks, Simon -- Simon Steele Programmers Notepad - http://www.pnotepad.org/Check your tests? The second two should work fine. The first one fails because the values are multiplied as longs and then returned as a uint64. The multiplication as a long wraps. Casting one value to a uint64 will make the multiplication occur on uint64's and should give correct results. Your message mentions 0xba5da800 * 0x38E = 0x29678EB3000 at the top and your tests use 0xba5e3500 * 1000... The code below seeems to give the results you expect.
typedef unsigned __int64 uint64_t; unsigned long myVal = 0xba5da800 ; uint64_t myfunction1(unsigned long val) { return val * 0x38E; } uint64_t myfunction2(unsigned long val) { return ((uint64_t)val * (uint64_t)0x38E); } uint64_t myfunction3(unsigned long val) { return (uint64_t)((uint64_t)val * (uint64_t)0x38E); } int main(int argc, char* argv[]) { uint64_t result1 = myfunction1(myVal); uint64_t result2 = myfunction2(myVal); uint64_t result3 = myfunction3(myVal); printf("myfunction1 - 0x%I64x\n", result1); printf("myfunction2 - 0x%I64x\n", result2); printf("myfunction3 - 0x%I64x\n", result3); return 0; }
Len Holgate www.jetbyte.com The right code, right now. -
Hi, I'm having a frustrating little problem with 64-bit integer arithmetic. It goes something like this: 0xba5da800 * 0x38E which should equal: 0x29678EB3000 Unfortunately, the code is a bit like this:
#define unsigned __int64 uint64_t unsigned long myval = 0xba5e3500; uint64_t myfunction(unsigned long val) { return val * 1000; }
This doesn't work, nor does:return ((uint64_t)val * (uint64_t)1000);
orreturn (uint64_t)((uint64_t)val * (uint64_t)1000);
The value I get returned looks like: 0xfffffeefffff0800 (or similar) Now, I've run into problems before with not casting everything into __int64 twice (ok I exadurate slightly) for every calculation - but this is weird. It seems that casting val to uint64_t is turning it into: 0xffffffffba5e3500 Why is this happening?! I truly do hate __int64, any help would be appreciated. thanks, Simon -- Simon Steele Programmers Notepad - http://www.pnotepad.org/Hi, thanks for saying that it works for you, it led me to the problem. The value being passed in was actually a long not an unsigned long, I didn't notice because it's actually part of a structure the definition of which has changed. "it does work, you fool!" posts are just as useful as solution advice so thanks to those who replied. I'll write a test harness next time before posting, my bad :) thanks again, simon. -- Simon Steele Programmers Notepad - http://www.pnotepad.org/