I'm writing a program that generates large prime numbers. Bigger than 2^32. How do I generate such big numbers in 32-bit C++? I use Windows 7 on 32-bit processor. I know I could get 64-bit support buy buying a new 64-bit computer, but it's not an option currently.
Asked
Active
Viewed 1,890 times
1
-
Haha, good one. You don't need to buy a new computer in order to be able to declare have 64-bit variables in your code. – barak manos Oct 19 '14 at 03:34
-
Possible duplicate of [this](http://stackoverflow.com/questions/18060242/how-to-store-extremely-large-numbers) – Nard Oct 19 '14 at 03:35
4 Answers
4
Use long long
or include stdint.h
or cstdint
and use int64_t
and uint64_t
.
In addition to this, you can refer to Windows data types at http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx
From this list, you can use DWORDLONG
, DWORD64
or INT64
.

doptimusprime
- 9,115
- 6
- 52
- 90
2
To print the number, use %lld
with printf:
long long variable;
printf( "your long long variable: %lld", variable );

bobobobo
- 64,917
- 62
- 258
- 363
1
Use long long
, which is at least 64-bit, and it's available in 32-bit machines as well.

Yu Hao
- 119,891
- 44
- 235
- 294
-
Strictly speaking, `long long` is at least 8 bits and at least as large as `long`. Nothing more is guaranteed by the language. – Don Reba Oct 19 '14 at 03:39
-
2@DonReba: The standard guarantees a minimum range of representable values for `long long`. A range which can only be satisfied by 64 bits or more. – Benjamin Lindley Oct 19 '14 at 03:44
-
@BenjaminLindley, I don't see it. Where does the standard do that? "Objects declared as characters (char) shall be large enough to store any member of the implementation's basic character set." (C++11 3.9.1.1) That's it for minimum ranges. – Don Reba Oct 19 '14 at 03:53
-
@DonReba: The C99 standard library defines the values in 5.2.4.2.1, which defines the contents of the `
` header. The C++11 standard, in 18.3.3, references the C99 standard, explicitly stating the the contents of the ` – Benjamin Lindley Oct 19 '14 at 04:05` header are the same as ` ` in the C standard. -
@BenjaminLindley It also notes that "the types of the constants defined by macros in
are not required to match the types to which the macros refer." The header does not place any constraints on the fundamental C++ types. – Don Reba Oct 19 '14 at 04:26 -
@DonReba: I don't see the relevance of that quote. All it is saying is that, for example, `SHRT_MIN` (which refers to the type `short int`) might be defined as the value `-32767`, even though that numeric constant is not of type `short int`, but rather of type `int`. – Benjamin Lindley Oct 19 '14 at 04:36
0
There also is the GMP library for signed integers of arbitrary size even >2^64. The C++ interface makes the variables behave just like normal ints via operator overloading.

Oncaphillis
- 1,888
- 13
- 15