a byte-wide integer

U

Uno

Is there a portable way to get a 4-byte wide int in C?

How about one that works on gcc?
 
P

Peter Nilsson

Ian Collins said:

True, but there is a C99 standard way of getting a 32-bit,
two's complement integer, if available.
because the number of bits in a byte isn't portable.

That isn't relevant. A 4 byte int is a 4 byte int, regardless
of how many bits there are in a byte.
If you stick to POSIX (CHAR_BIT=8), int32_t.

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.

[Of course, the OP almost certainly _really_ wants a 32-bit
integer, rather than one that is 4 bytes wide.]
 
I

Ian Collins

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.

It is in the context of the C99 types. int32_t may be one byte wide.
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.

That's why I said "If you stick to POSIX".
 
S

SG

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
 
P

Philipp Klaus Krause

Am 25.05.2010 13:30, schrieb SG:
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

Why not just use standard uint_least32_t / int_least32_t from stdint.h?

Philipp
 
S

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?

Sure, that's an option. But it's not part of ISO C90 nor ISO C++03. As
far as I know Microsoft compilers prior to Visual Studio 2010 did not
support this C99 header. So, if you care about whether your program
can be compiled with a C90 compiler...

Cheers,
SG
 
E

Eric Sosman

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;
struct { unsigned int ui : 4; } y;
x.si = -3;
y.ui = 13;
How about one that works on gcc?

Both forms work with all Standard-conforming C compilers.
 
E

Eric Sosman

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;
[...]

Sorry; forget it; I somehow misread "byte" as "bit." I
should be a lytelle byte more careful lest I make myself
unduly vulnerable to cryteycysm.
 
B

Ben Bacarisse

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;

I think you misread "4-byte wide" as "4-bit wide"?

<snip>
 
U

Uno

Ben said:
I think you misread "4-byte wide" as "4-bit wide"?

<snip>

Thanks all for replies. It's not something you can do in fortran
without using the iso_c_binding. Next time I get a lot of computing
time, I'll see if I can use a fortran caller to get such a creature.

Cheers,
 

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,093
Messages
2,570,613
Members
47,230
Latest member
RenaldoDut

Latest Threads

Top