Int Size Question

I

Immortal Nephi

I always use unsigned int to do math calculation or algorithm. The
int size was either 16 bit or 32 bit on older machine and older C++
Compiler. Today, int size is either 32 bit or 64 bit. The int size
is not important to me when math calculation or algorithm in my C++
code is performed maximum 16 bit. It does have enough room to either
int 32 bit or int 64 bit without 16 bit overflow.

My C++ code can be ported to any C++ Compiler on depend of different
machines. If I choose int 64 bit to do math calculation or algorithm,
how do I tell C++ Compiler to use int 64 bit instead of int 32 bit
with the answer -- sizeof? I do not have to remodify int size in all
header files and source code files.

Same example, char is always 8 bit, but one machine may have 9 bit or
10 bit width. To create string array in memory, each element is
always 8 bit. If 9 bit or 10 bit width is detected, then I must
accommodate to put 9 bit or 10 bit string array into 16 bit string
array.
 
S

Steve Wolter

My C++ code can be ported to any C++ Compiler on depend of different
machines. If I choose int 64 bit to do math calculation or algorithm,
how do I tell C++ Compiler to use int 64 bit instead of int 32 bit
with the answer [...]

The stdint.h header offers integer types with guaranteed sizes.
For example, uint8_t is an integer type 8 bits wide and unsigned.

Regards, Steve
 
J

Juha Nieminen

Steve said:
My C++ code can be ported to any C++ Compiler on depend of different
machines. If I choose int 64 bit to do math calculation or algorithm,
how do I tell C++ Compiler to use int 64 bit instead of int 32 bit
with the answer [...]

The stdint.h header offers integer types with guaranteed sizes.
For example, uint8_t is an integer type 8 bits wide and unsigned.

Note that stdint.h was only standardized in C99, and consequently not
yet part of the official C++ standard. Many C++ compilers support it,
though.
 
A

Alf P. Steinbach

* Juha Nieminen:
Steve said:
My C++ code can be ported to any C++ Compiler on depend of different
machines. If I choose int 64 bit to do math calculation or algorithm,
how do I tell C++ Compiler to use int 64 bit instead of int 32 bit
with the answer [...]
The stdint.h header offers integer types with guaranteed sizes.
For example, uint8_t is an integer type 8 bits wide and unsigned.

Note that stdint.h was only standardized in C99, and consequently not
yet part of the official C++ standard. Many C++ compilers support it,
though.

There is equivalent functionality in Boost.


Cheers,

- Alf
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top