U
Uno
Is there a portable way to get a 4-byte wide int in C?
How about one that works on gcc?
How about one that works on gcc?
Is there a portable way to get a 4-byte wide int in C?
How about one that works on gcc?
Uno said:Is there a portable way to get a 4-byte wide int in C?
then use int32_t. If the system has said:How about one that works on gcc?
Ian Collins said:
because the number of bits in a byte isn't portable.
If you stick to POSIX (CHAR_BIT=8), int32_t.
True, but there is a C99 standard way of getting a 32-bit,
two's complement integer, if available.
That isn't relevant. A 4 byte int is a 4 byte int, regardless
of how many bits there are in a byte.
int32_t is an optional type available in C99. It needn't be
4 bytes wide, it may be 2 or even 1 byte in size.
Richard said:If what you really want is an int that is at least 32 bits wide, just
use long int. That's guaranteed to be at least 32 bits, and it works in
*all* conforming compilers, C90 as well as C99.
I've often used something like this (in some dedicated header file):
#include <limits.h>
#if UINT_MAX < 4294967295
typedef unsigned long int uint_lst32;
typedef signed long int int_lst32;
#else
typedef unsigned int uint_lst32;
typedef signed int int_lst32;
#endif
to get a typedef for integers of at least 32 bits. This avoids using
long ints if ints are already big enough. In some cases this might
matter. On some platform long could be 64 bits wide and if all you
need is an int with 32 bits ...
Cheers,
SG
Am 25.05.2010 13:30, schrieb SG:I've often used something like this (in some dedicated header file):
[...]
to get a typedef for integers of at least 32 bits. [...]
Why not just use standard uint_least32_t / int_least32_t from stdint.h?
Is there a portable way to get a 4-byte wide int in C?
How about one that works on gcc?
Is there a portable way to get a 4-byte wide int in C?
Yes, although you'll need to wrap it in a struct (or union):
struct { signed int si : 4; } x;
[...]
Eric Sosman said:Yes, although you'll need to wrap it in a struct (or union):
struct { signed int si : 4; } x;
struct { unsigned int ui : 4; } y;
x.si = -3;
y.ui = 13;
Ben said:I think you misread "4-byte wide" as "4-bit wide"?
<snip>
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.