Portable way to use 64-bit values

S

Sorav Bansal

The C standard does not seem to support integer types which are 64-bits
long. 'long long' and 'uint64_t' work only on limited
platforms/architectures.

What is the best way to use 64-bit values in compliance with the C-standard?

Regards
 
D

David Resnick

Sorav said:
The C standard does not seem to support integer types which are 64-bits
long. 'long long' and 'uint64_t' work only on limited
platforms/architectures.

What is the best way to use 64-bit values in compliance with the C-standard?

Regards

If you mean the older C89 standard, you might use a portable bignum
library. Perhaps overkill for your purposes?

If all your target platforms support 64 bits in some flavor, you
could use typedefs to smooth out the differences -- sketchy example:

#ifdef _WIN32
typedef my_longlongint __int64
#else
typedef my_longlongint long long int
#endif

-David
 
E

Emmanuel Delahaye

Sorav Bansal wrote on 15/07/05 :
The C standard does not seem to support integer types which are 64-bits long.
'long long' and 'uint64_t' work only on limited platforms/architectures.

What is the best way to use 64-bit values in compliance with the C-standard?

Use a c99 compiler, like gcc 3.x or more. Works on most known targets.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
S

S.Tobias

David Resnick said:
#ifdef _WIN32
typedef my_longlongint __int64
#else
typedef my_longlongint long long int
#endif

ITYM:
typedef __int64 my_longlongint;
and
typedef long long int my_longlongint;
 
D

Dave

Sorav said:
The C standard does not seem to support integer types which are 64-bits
long. 'long long' and 'uint64_t' work only on limited
platforms/architectures.

What is the best way to use 64-bit values in compliance with the
C-standard?

Regards

Define "best." For example, one way is to represent numbers that are
too big for the compiler's integer types as char arrays, where each char
could represent 0-9, or 2 BCD digits (hex 00-99), or even 'digits' in
base 256. With this approach you could guarantee to be 100% portable.
 
D

David Resnick

S.Tobias said:
ITYM:
typedef __int64 my_longlongint;
and
typedef long long int my_longlongint;

Yep, of course. Written too late at night. Thanks for the correction.

-David
 
A

Alexei A. Frounze

Dave said:
Define "best." For example, one way is to represent numbers that are
too big for the compiler's integer types as char arrays, where each char
could represent 0-9, or 2 BCD digits (hex 00-99), or even 'digits' in
base 256. With this approach you could guarantee to be 100% portable.

Better base 256.
Alternatively one could write a little library to handle int64's composed of
two int32's. Provided one can implement it correctly :) Actually, the nice
solution would be to use a C++ class for this composed int64 type.

Alex
 
M

Malcolm

Sorav Bansal said:
The C standard does not seem to support integer types which are 64-bits
long. 'long long' and 'uint64_t' work only on limited
platforms/architectures.

What is the best way to use 64-bit values in compliance with the C-
standard?
There isn't one. Almost always you will want a reasonably fast operation,
which rules out big integer solutions which operate on arrays of bits.

The best bet is to use "long long", and accept that sometimes the code will
refuse to compile.

If you need to emulate 64 bits on non-64 bit processors, use inline
assembly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top