I
Ian Collins
Works for ptrdiff_t, but not for a short.
So change the types:
#define MAX_OF(type) (type)(~(1UL<<(sizeof(type)*CHAR_BIT-1)))
Works for ptrdiff_t, but not for a short.
John Kelly said:Here's another try at the elusive universal solution for the maximum
value of any signed type.
# define MAX_SIGNED_BITS(type) (sizeof (type) * CHAR_BIT - 1)
# define MAX_SIGNED(type, name) \
\
type \
name ## _max_ (void)\
{ \
type last; \
int tn, bits = MAX_SIGNED_BITS(type); \
for (last = 1, tn = 1; tn < bits; tn++) { \
last = last * 2 + 1; \
} \
return last; \
}
MAX_SIGNED (short, short);
MAX_SIGNED (ptrdiff_t, ptrdiff_t);
MAX_SIGNED (long long, long_long);
int
main (void)
{
short const short_max = short_max_ ();
ptrdiff_t const ptrdiff_t_max = ptrdiff_t_max_ ();
long long const long_long_max = long_long_max_ ();
printf ("short_max = %d\n", short_max);
printf ("ptrdiff_t_max = %d\n", ptrdiff_t_max);
So change the types:
#define MAX_OF(type) (type)(~(1UL<<(sizeof(type)*CHAR_BIT-1)))
Why the loop? The result is statically determined from the value of
MAX_SIGNED_BITS(type). If the type has padding bits, this method does
not work.
If you are happy with a solution that assumes there are no
padding bits, then a version has already been posted that produces
a compile-time constant.
No need for a function nor a loop.
This is undefined. In C99 there is the t size modifier and in C90
ptrdiff_t can't be wider than long
John Kelly said:I can't find much info on the padding bits you mention. Can you
elaborate?
Ian's solution did not work for me, when I tried it with long long.
Does it work for you?
Printing formats aside, how does the t size modifier help me derive the
maximum positive value for a signed type?
And In C90, what guarantees
that ptrdiff_t is not shorter than long?
I'm looking for a solution that works with any signed type, not just
ptrdiff_t, for C90 or C99.
I don't know. If you re-post it I'll check. I was talking about the
expression I posted:
6.2.6.2. Basically they are bits that have to be included in the size
of the object but which don't contribute to the value.
I can see how that might be true for a pointer, but it makes no sense to
implement an integer type that way.
Ignoring the possibility of padding bits or not?
If there is no way to determine the number of paddling bits for a given
type, and thus its range, that is a flaw in the standard.
(((t)1 << sizeof(t) * CHAR_BIT - 2) - 1 + \
((t)1 << sizeof(t) * CHAR_BIT - 2))
Ignoring the possibility of padding bits or not?
I can't get it to work for long long. Does it work for you?
Change the 1UL to 1ULL. But remember both "1ULL" and long long are from
C99.
Ian Collins said:On 08/27/10 02:46 AM, John Kelly wrote: [...]I can't get it to work for long long. Does it work for you?
Change the 1UL to 1ULL. But remember both "1ULL" and long long are from
C99.
If '__STDC_VERSION__' is '199901L', you can use 'SIZE_MAX' orJohn said:After taking a closer look at this, I see what it's doing. Good
solution. My compiler warned about parentheses on the shift, so I added
some to make it quiet.
Well, since the standard does not provide any way of determining the
number of padding bits for a given type, and thus its true range, what
else can I do, but ignore them?
John Kelly said:Well, since the standard does not provide any way of
determining the number of padding bits for a given type,
Shao Miller said:In C89, I believe we have in A.6.3.6 that 'size_t' is "The type of
integer required to hold the maximum size of an array".
Determining the number of padding bits in an unsigned
type is simple in both C90 and C99. Since there's a way to
determine the maximum type of any signed type in C90, there
is therefore a way of determining the number of padding bits.
John Kelly said:Are they family secrets?
John Kelly said:If there is no way to determine the number of paddling bits for a given
type, and thus its range, that is a flaw in the standard.
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.