![]() |
![]() |
#1 |
Jan 2005
Transdniestr
7678 Posts |
![]()
Hi,
I'm using Mingw version 5.1.6 within Windows XP (32-bit). I'd like to use 64-bit integers within C code but I'm running into problems. unsigned long z = 2*3*4*5*6*7*8*9*10*11*12; long long unsigned int zz = z*13*14; printf("Here is zz: %I64u\n"; After checking on-line, I found that the correct format to use is (the Microsoft-specific) %I64u, but this prints the lower 32 bits only. (Note: making z a "long long unsigned int" doesn't change the output) ============================================== I found a different problem doing simple arithmetic: unsigned long z = 2*3*4*5*6*7*8*9*10*11*12; long long unsigned int zz = z*13*14; long long unsigned int b = 175*z; long long unsigned int c = zz-b; // 7 * factorial(12) printf("c is %I64u\n",c); The correct answer is only: 3353011200 (< 2^32) but this code prints out an incorrect number (18446744072767595220) that is more greater than 2^32. ////////////////////////////////////////////////////////// Any insight would be appreciated. Thanks. (The background is that I have some gmplib code that I wrote which I want to speed up, if possible. I think I could do so by replacing some of the "big ints" with long longs.) |
![]() |
![]() |
![]() |
#2 |
"Robert Gerbicz"
Oct 2005
Hungary
1,621 Posts |
![]()
"long long unsigned int" is in wrong order. I think you want:
"unsigned long long int". And if %I64u doesn't works then try the other format: %llu. One of them should be good. |
![]() |
![]() |
![]() |
#3 |
Jan 2005
Transdniestr
503 Posts |
![]()
Sorry, I see my error.
I was doing calculations with an "unsigned long int" and wasn't doing an apparently necessary cast to "unsigned long long int". Both using the long long type and explicitly casting the long to it worked. %I64u is definitely the format to use in this scenario. And it turns out, the position of "unsigned" doesn't matter. Thanks for your feedback though. ===================================== Unfortunately, it looks like there isn't built-in functions to initialize/set an mpz_t to a long long. mpz_init_set_ui(a,b) just sets a to the lower 32-bits of b. Last fiddled with by grandpascorpion on 2009-10-01 at 00:41 |
![]() |
![]() |
![]() |
#4 | |
"Mark"
Apr 2003
Between here and the
37·191 Posts |
![]() Quote:
Also if you are using MinGW, there is probably a header called stdint.h. This has a predefined uint64_t variable type. Finally, I try to avoid using "int" because some compilers treat an int as a 16 bit value (aka short int), even though the CPU and OS are 32-bit. You could use long, which will be 32 bits or 64 bits (depending upon the compiler, OS, and compiler options). I don't know if any compiler defines a long as 16 bits. I always use stdint.h to guarantee portability. Unfortunately M$ doesn't have such a header in Visual Studio, but you can #typedef those datatypes using WORD and DWORD appropriately to ensure portability. Last fiddled with by rogue on 2009-10-01 at 00:50 |
|
![]() |
![]() |
![]() |
#5 | |
Jan 2005
Transdniestr
503 Posts |
![]() Quote:
It's unlikely I would port it but if so I could I always have conditional compilation based on that format string. |
|
![]() |
![]() |
![]() |
#6 |
Tribal Bullet
Oct 2004
23×5×89 Posts |
![]()
If you include inttypes.h, you get macros to insert into format strings that will print 64-bit values correctly. On my mingw installation they are
Code:
inttypes.h:#define PRId64 "I64d" inttypes.h:#define PRIi64 "I64i" inttypes.h:#define PRIo64 "I64o" inttypes.h:#define PRIu64 "I64u" inttypes.h:#define PRIx64 "I64x" inttypes.h:#define PRIX64 "I64X" printf("I have computed %" PRId64 "\n", answer); Last fiddled with by jasonp on 2009-10-01 at 01:28 |
![]() |
![]() |
![]() |
#7 | |
Jan 2005
Transdniestr
1111101112 Posts |
![]() Quote:
|
|
![]() |
![]() |
![]() |
#8 | |
Jan 2008
France
10010101002 Posts |
![]() Quote:
|
|
![]() |
![]() |
![]() |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
How long is too long? | ThomRuley | Msieve | 3 | 2013-11-30 04:52 |
very long int | davar55 | Lounge | 60 | 2013-07-30 20:26 |
How long will the project take? | lycorn | Lounge | 24 | 2013-01-15 01:53 |
I think it's gonna be a long, long time | panic | Hardware | 9 | 2009-09-11 05:11 |
How long is too long? | schickel | Lounge | 2 | 2009-02-22 12:31 |