Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > using pre-processor to count bits in integer types...

Reply
Thread Tools

using pre-processor to count bits in integer types...

 
 
Chris M. Thomasson
Guest
Posts: n/a
 
      01-15-2009
How "reliable" is the following code:
__________________________________________________ __________
#include <stdio.h>
#include <limits.h>


#define COUNT_BITS_X_CALC(mp_value, mp_level) \
(! ((mp_value) >> (CHAR_BIT * (mp_level)))) \
? (CHAR_BIT * (mp_level))


#define COUNT_BITS_X(mp_value) ( \
(COUNT_BITS_X_CALC(mp_value, 1) \
: (COUNT_BITS_X_CALC(mp_value, 2) \
: (COUNT_BITS_X_CALC(mp_value, 4) \
: (COUNT_BITS_X_CALC(mp_value, 5) \
: (COUNT_BITS_X_CALC(mp_value, 6) \
: (COUNT_BITS_X_CALC(mp_value, 7) \
: (COUNT_BITS_X_CALC(mp_value, \
: (COUNT_BITS_X_CALC(mp_value, 9) \
: (COUNT_BITS_X_CALC(mp_value, 10) \
: 0))))))))) \
)


#define COUNT_BITS(mp_value) COUNT_BITS_X(mp_value)


#if (COUNT_BITS(USHRT_MAX) == 16)
typedef short int int16_type;
typedef unsigned short int uint16_type;
#else
# error Platform does not provide an exact 16-bit integer! ;^(...
#endif


int main(void) {
printf(
"char is %lu bits\n"
"short is %lu bits\n"
"int is %lu bits\n"
"long is %lu bits\n"
"int16_type is %lu bits\n",
(unsigned long int)COUNT_BITS(UCHAR_MAX),
(unsigned long int)COUNT_BITS(USHRT_MAX),
(unsigned long int)COUNT_BITS(UINT_MAX),
(unsigned long int)COUNT_BITS(ULONG_MAX),
(unsigned long int)(CHAR_BIT * sizeof(int16_type))
);

return 0;
}
__________________________________________________ __________




I am tinkering around with different ways to use the pre-processor to
determine exact number of bits in integral types. Is there a much better way
to do this? What am I missing here?




Thanks.

 
Reply With Quote
 
 
 
 
Spiros Bousbouras
Guest
Posts: n/a
 
      01-15-2009
On 15 Jan, 22:40, "Chris M. Thomasson" <(E-Mail Removed)> wrote:
> How "reliable" is the following code:
> __________________________________________________ __________
> #include <stdio.h>
> #include <limits.h>
>
> #define COUNT_BITS_X_CALC(mp_value, mp_level) \
> (! ((mp_value) >> (CHAR_BIT * (mp_level)))) \
> ? (CHAR_BIT * (mp_level))
>
> #define COUNT_BITS_X(mp_value) ( \
> (COUNT_BITS_X_CALC(mp_value, 1) \
> : (COUNT_BITS_X_CALC(mp_value, 2) \
> : (COUNT_BITS_X_CALC(mp_value, 4) \
> : (COUNT_BITS_X_CALC(mp_value, 5) \
> : (COUNT_BITS_X_CALC(mp_value, 6) \
> : (COUNT_BITS_X_CALC(mp_value, 7) \
> : (COUNT_BITS_X_CALC(mp_value, \
> : (COUNT_BITS_X_CALC(mp_value, 9) \
> : (COUNT_BITS_X_CALC(mp_value, 10) \
> : 0))))))))) \
> )
>
> #define COUNT_BITS(mp_value) COUNT_BITS_X(mp_value)
>
> #if (COUNT_BITS(USHRT_MAX) == 16)
> typedef short int int16_type;
> typedef unsigned short int uint16_type;
> #else
> # error Platform does not provide an exact 16-bit integer! ;^(...
> #endif
>
> int main(void) {
> printf(
> "char is %lu bits\n"
> "short is %lu bits\n"
> "int is %lu bits\n"
> "long is %lu bits\n"
> "int16_type is %lu bits\n",
> (unsigned long int)COUNT_BITS(UCHAR_MAX),
> (unsigned long int)COUNT_BITS(USHRT_MAX),
> (unsigned long int)COUNT_BITS(UINT_MAX),
> (unsigned long int)COUNT_BITS(ULONG_MAX),
> (unsigned long int)(CHAR_BIT * sizeof(int16_type))
> );
>
> return 0;}
>
> __________________________________________________ __________
>
> I am tinkering around with different ways to use the pre-processor to
> determine exact number of bits in integral types. Is there a much better way
> to do this? What am I missing here?


You are missing number 3 from your list of possibilities! The
same thing could be written much more clearly by checking
whether the value you are testing is equal to 2^n - 1 for
successive values of n. You would need to write a larger list
but it would look clearer. For example

#define COUNT_BITS(n) ( \
(n) == 255 ? 8 : \
(n) == 511 ? 9 : \
(n) == 1023 ? 10 : \
.....
)

A bit more complicated but still (IMO) clearer is

#define COUNT_BITS(n) ( \
(n) == ( (1 << CHAR_BIT) -1) ? CHAR_BIT : \
(n) == ( (1 << 2*CHAR_BIT) -1) ? 2*CHAR_BIT : \
(n) == ( (1 << 3*CHAR_BIT) -1) ? 3*CHAR_BIT : \
.....
)

(Neither one tested)

I have a vague recollection that a while ago someone posted a
macro which did what you wanted (or something close) and could
handle ridiculously large numbers of bits. Sadly I haven't
stored the link and some Google search didn't find the thread
I was looking for. I would love to see that again.

But it would be even simpler if you didn't use the preprocessor
but did it during runtime in the beginning of your programme. Is
there any practical reason you want to use the preprocessor ?


 
Reply With Quote
 
 
 
 
Chris M. Thomasson
Guest
Posts: n/a
 
      01-16-2009
"Eric Sosman" <(E-Mail Removed)> wrote in message
news:1232061810.681510@news1nwk...
> Chris M. Thomasson wrote:
>> How "reliable" is the following code:
>> [... see up-thread ...]

>
> "Reliable" in the face of what threats or eventualities?
> It doesn't work in the presence of padding bits, it might not
> work for UINTMAX_MAX or INTMAX_MAX, it definitely won't work for
> floating-point types ... I'm not sure what realm of possibilities
> you're concerned about.


I would only ever pass the following values to the `COUNT_BITS' macro:


UCHAR_MAX
USHRT_MAX
UINT_MAX
ULONG_MAX


I am assuming that the number of bits it takes to hold any of those values
will be a multiple of `CHAR_BIT'.




>> [...]
>> I am tinkering around with different ways to use the pre-processor to
>> determine exact number of bits in integral types. Is there a much better
>> way to do this? What am I missing here?

>
> The total number of bits in a type is `sizeof(type) * CHAR_BIT'.
> Excluding possible padding bits seems difficult. I guess you could
> change your procedure to increase the shift distances by one bit at
> at time instead of CHAR_BIT, and test for equality to one instead of
> zero (to guard against a too-wide shift), but you've still got to
> figure out how many tests to make: C99's integers go up to at least
> 64 bits, and possibly beyond.


I could shift by one. Humm, well I also could add support for a couple of
hundred bits. This macro would be contained in a dedicated header file, so
its not that bad.




> The "much better way" -- well, can you formulate your questions
> in terms of ranges rather than in terms of logarithms? That is, how
> badly do you need actual bit counts?


This macro would only get invoked when the end user build system for my
library determines that its being compiled on an "unknown" platform.
Something like:




/* try to determine operating system */
/* [...] */


/* try to determine architecture */
/* [...] */


/* try to determine compiler */
#if defined (COMPILER_VENDOR_1)
# include "compiler/vendor/1/core.h"
#elif defined (COMPILER_VENDOR_2)
# include "compiler/vendor/2/core.h"
#elif
# include "compiler/vendor/unknown/core.h"
#endif




The `compiler/vendor/.../core.h' files are required to define some types
that have exact number of bits. If I know the compiler, I will use specific
extensions to get the job done. However, if the compiler is unknown, well, I
need to at least "attempt" to render the correctly sized types. I need this
to all be performed at compile time.

 
Reply With Quote
 
Spiros Bousbouras
Guest
Posts: n/a
 
      01-16-2009
On 16 Jan, 00:00, "Chris M. Thomasson" <(E-Mail Removed)> wrote:
> This macro would only get invoked when the end user build system for my
> library determines that its being compiled on an "unknown" platform.
> Something like:
>
> /* try to determine operating system */
> /* [...] */
>
> /* try to determine architecture */
> /* [...] */
>
> /* try to determine compiler */
> #if defined (COMPILER_VENDOR_1)
> # include "compiler/vendor/1/core.h"
> #elif defined (COMPILER_VENDOR_2)
> # include "compiler/vendor/2/core.h"
> #elif
> # include "compiler/vendor/unknown/core.h"
> #endif
>
> The `compiler/vendor/.../core.h' files are required to define some types
> that have exact number of bits. If I know the compiler, I will use specific
> extensions to get the job done. However, if the compiler is unknown, well, I
> need to at least "attempt" to render the correctly sized types. I need this
> to all be performed at compile time.


You could run a programme which tests these things,
writes a configuration file with preprocessor directives
and then the compilation of your library begins. So
basically a much simpler version of what autoconf does.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      01-16-2009
Spiros Bousbouras <(E-Mail Removed)> wrote:
> ...
> A bit more complicated but still (IMO) clearer is
>
> #define COUNT_BITS(n) ( * * * * * * * * * * * * * \
> (n) == ( (1 << CHAR_BIT) -1) ? CHAR_BIT : * * * * \
> (n) == ( (1 << 2*CHAR_BIT) -1) ? 2*CHAR_BIT *: * *\
> (n) == ( (1 << 3*CHAR_BIT) -1) ? 3*CHAR_BIT : * * \


There are a few problems with this:
n may be a signed type
n may be a padded type
n may be wider than the left shifted 1 (int)
[Even 1<<CHAR_BIT can potentially invoke UB.]

<snip>
> I have a vague recollection that a while ago someone
> posted a macro which did what you wanted (or something
> close) and could handle ridiculously large numbers of
> bits.


Hallvard B Furuseth once posted the following (which
I've edited slightly due to line wrap issues)...

/* Number of bits in inttype_MAX, or in any (1<<b)-1
where 0 <= b < 3E+10 */
#define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) \
/0x3fffffffL %0x3fffffffL *30 + \
(m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4 - \
12/((m)%31+3))

--
Peter
 
Reply With Quote
 
Spiros Bousbouras
Guest
Posts: n/a
 
      01-16-2009
On 16 Jan, 00:28, Peter Nilsson <(E-Mail Removed)> wrote:
> Spiros Bousbouras <(E-Mail Removed)> wrote:
>
> > I have a vague recollection that a while ago someone
> > posted a macro which did what you wanted (or something
> > close) and could handle ridiculously large numbers of
> > bits.

>
> Hallvard B Furuseth once posted the following (which
> I've edited slightly due to line wrap issues)...
>
> /* Number of bits in inttype_MAX, or in any (1<<b)-1
> where 0 <= b < 3E+10 */
> #define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) \
> /0x3fffffffL %0x3fffffffL *30 + \
> (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4 - \
> 12/((m)%31+3))


Thank you. This had been bugging me for a while.

 
Reply With Quote
 
Chris M. Thomasson
Guest
Posts: n/a
 
      01-16-2009

"Eric Sosman" <(E-Mail Removed)> wrote in message
news:1232061810.681510@news1nwk...
> Chris M. Thomasson wrote:
>> How "reliable" is the following code:
>> [... see up-thread ...]

>
> "Reliable" in the face of what threats or eventualities?
> It doesn't work in the presence of padding bits, it might not
> work for UINTMAX_MAX or INTMAX_MAX, it definitely won't work for
> floating-point types ... I'm not sure what realm of possibilities
> you're concerned about.
>
>> [...]
>> I am tinkering around with different ways to use the pre-processor to
>> determine exact number of bits in integral types. Is there a much better
>> way to do this? What am I missing here?

>
> The total number of bits in a type is `sizeof(type) * CHAR_BIT'.
> Excluding possible padding bits seems difficult. I guess you could
> change your procedure to increase the shift distances by one bit at
> at time instead of CHAR_BIT, and test for equality to one instead of
> zero (to guard against a too-wide shift), but you've still got to
> figure out how many tests to make: C99's integers go up to at least
> 64 bits, and possibly beyond.
>
> The "much better way" -- well, can you formulate your questions
> in terms of ranges rather than in terms of logarithms? That is, how
> badly do you need actual bit counts?


Let post code with a shift by one:
__________________________________________________ ____________________
#include <limits.h>
#include <stdio.h>


#define CHECK_BIT_X(n, b) (((n) >> (b)) == 1) ? (b) + 1


#define COUNT_BITS_CALC_X(n, b) ( \
(CHECK_BIT_X(n, (b) + 0) : (CHECK_BIT_X(n, (b) + 1) \
: (CHECK_BIT_X(n, (b) + 2) : (CHECK_BIT_X(n, (b) + 3) \
: (CHECK_BIT_X(n, (b) + 4) : (CHECK_BIT_X(n, (b) + 5) \
: (CHECK_BIT_X(n, (b) + 6) : (CHECK_BIT_X(n, (b) + 7) \
: (CHECK_BIT_X(n, (b) + : (CHECK_BIT_X(n, (b) + 9) \
: (CHECK_BIT_X(n, (b) + 10) : (CHECK_BIT_X(n, (b) + 11) \
: (CHECK_BIT_X(n, (b) + 12) : (CHECK_BIT_X(n, (b) + 13) \
: (CHECK_BIT_X(n, (b) + 14) : (CHECK_BIT_X(n, (b) + 15) \
: (CHECK_BIT_X(n, (b) + 16) : (CHECK_BIT_X(n, (b) + 17) \
: (CHECK_BIT_X(n, (b) + 1 : (CHECK_BIT_X(n, (b) + 19) \
: (CHECK_BIT_X(n, (b) + 20) : (CHECK_BIT_X(n, (b) + 21) \
: (CHECK_BIT_X(n, (b) + 22) : (CHECK_BIT_X(n, (b) + 23) \
: (CHECK_BIT_X(n, (b) + 24) : (CHECK_BIT_X(n, (b) + 24) \
: (CHECK_BIT_X(n, (b) + 26) : (CHECK_BIT_X(n, (b) + 26) \
: (CHECK_BIT_X(n, (b) + 2 : (CHECK_BIT_X(n, (b) + 2 \
: (CHECK_BIT_X(n, (b) + 30) : (CHECK_BIT_X(n, (b) + 31) \
: (CHECK_BIT_X(n, (b) + 32) : (CHECK_BIT_X(n, (b) + 33) \
: (CHECK_BIT_X(n, (b) + 34) : (CHECK_BIT_X(n, (b) + 35) \
: (CHECK_BIT_X(n, (b) + 36) : (CHECK_BIT_X(n, (b) + 37) \
: (CHECK_BIT_X(n, (b) + 3 : (CHECK_BIT_X(n, (b) + 39) \
: (0))))))))))))))))))))))))))))))))))))))))) \
)


#define COUNT_BITS_X(n, b) \
COUNT_BITS_CALC_X(n, b) ? COUNT_BITS_CALC_X(n, b)


#define COUNT_BITS(n) ( \
(COUNT_BITS_X(n, 0) : (COUNT_BITS_X(n, 40) \
: (COUNT_BITS_X(n, 80) : (COUNT_BITS_X(n, 120) \
: (COUNT_BITS_X(n, 160) : (COUNT_BITS_X(n, 200) \
: (COUNT_BITS_X(n, 240) : (COUNT_BITS_X(n, 280) \
: (COUNT_BITS_X(n, 320) : (COUNT_BITS_X(n, 360) \
: (COUNT_BITS_X(n, 400) : (COUNT_BITS_X(n, 440) \
: (COUNT_BITS_X(n, 480) : (COUNT_BITS_X(n, 520) \
: (COUNT_BITS_X(n, 540) : (COUNT_BITS_X(n, 560) \
: (COUNT_BITS_X(n, 600) : (COUNT_BITS_X(n, 640) \
: (COUNT_BITS_X(n, 680) : (COUNT_BITS_X(n, 720) \
: (COUNT_BITS_X(n, 760) : (COUNT_BITS_X(n, 800) \
: (COUNT_BITS_X(n, 840) : (COUNT_BITS_X(n, 880) \
: (COUNT_BITS_X(n, 920) : (COUNT_BITS_X(n, 960) \
: (COUNT_BITS_X(n, 1000) : (COUNT_BITS_X(n, 1040) \
: (COUNT_BITS_X(n, 1080) : (COUNT_BITS_X(n, 1120) \
: (COUNT_BITS_X(n, 1160) : (COUNT_BITS_X(n, 1200) \
: (COUNT_BITS_X(n, 1240) : (COUNT_BITS_X(n, 1260) \
: (COUNT_BITS_X(n, 1300) : (COUNT_BITS_X(n, 1340) \
: (COUNT_BITS_X(n, 1380) : (COUNT_BITS_X(n, 1420) \
: (COUNT_BITS_X(n, 1460) : (COUNT_BITS_X(n, 1500) \
: (COUNT_BITS_X(n, 1540) : (COUNT_BITS_X(n, 1580) \
: (0))))))))))))))))))))))))))))))))))))))))))) \
)




#if (COUNT_BITS(UCHAR_MAX) ==
typedef signed char int8_type;
typedef unsigned char uint8_type;
#else
# error Platform does not provide an exact 8-bit integer!
#endif


#if (COUNT_BITS(USHRT_MAX) == 16)
typedef signed short int int16_type;
typedef unsigned char uint16_type;
#else
# error Platform does not provide an exact 16-bit integer!
#endif




int main(void) {
printf("%lu\n", COUNT_BITS(18446744073709551615));
return 0;
}
__________________________________________________ ____________________




This should go up to 1559 bit integers. Anyway, what do you think?

 
Reply With Quote
 
Chris M. Thomasson
Guest
Posts: n/a
 
      01-16-2009
"Peter Nilsson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
Spiros Bousbouras <(E-Mail Removed)> wrote:
> > ...
> > A bit more complicated but still (IMO) clearer is
> >
> > #define COUNT_BITS(n) ( \
> > (n) == ( (1 << CHAR_BIT) -1) ? CHAR_BIT : \
> > (n) == ( (1 << 2*CHAR_BIT) -1) ? 2*CHAR_BIT : \
> > (n) == ( (1 << 3*CHAR_BIT) -1) ? 3*CHAR_BIT : \


> There are a few problems with this:
> n may be a signed type
> n may be a padded type
> n may be wider than the left shifted 1 (int)
> [Even 1<<CHAR_BIT can potentially invoke UB.]


> <snip>
> > I have a vague recollection that a while ago someone
> > posted a macro which did what you wanted (or something
> > close) and could handle ridiculously large numbers of
> > bits.


> Hallvard B Furuseth once posted the following (which
> I've edited slightly due to line wrap issues)...


> /* Number of bits in inttype_MAX, or in any (1<<b)-1
> where 0 <= b < 3E+10 */
> #define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) \
> /0x3fffffffL %0x3fffffffL *30 + \
> (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4 - \
> 12/((m)%31+3))


That does not seem to work at all; here is an example:
__________________________________________________ ____________________
#include <limits.h>
#include <stdio.h>


#define CHECK_BIT_X(n, b) (((n) >> (b)) == 1) ? (b) + 1


#define COUNT_BITS_CALC_X(n, b) ( \
(CHECK_BIT_X(n, (b) + 0) : (CHECK_BIT_X(n, (b) + 1) \
: (CHECK_BIT_X(n, (b) + 2) : (CHECK_BIT_X(n, (b) + 3) \
: (CHECK_BIT_X(n, (b) + 4) : (CHECK_BIT_X(n, (b) + 5) \
: (CHECK_BIT_X(n, (b) + 6) : (CHECK_BIT_X(n, (b) + 7) \
: (CHECK_BIT_X(n, (b) + : (CHECK_BIT_X(n, (b) + 9) \
: (CHECK_BIT_X(n, (b) + 10) : (CHECK_BIT_X(n, (b) + 11) \
: (CHECK_BIT_X(n, (b) + 12) : (CHECK_BIT_X(n, (b) + 13) \
: (CHECK_BIT_X(n, (b) + 14) : (CHECK_BIT_X(n, (b) + 15) \
: (CHECK_BIT_X(n, (b) + 16) : (CHECK_BIT_X(n, (b) + 17) \
: (CHECK_BIT_X(n, (b) + 1 : (CHECK_BIT_X(n, (b) + 19) \
: (CHECK_BIT_X(n, (b) + 20) : (CHECK_BIT_X(n, (b) + 21) \
: (CHECK_BIT_X(n, (b) + 22) : (CHECK_BIT_X(n, (b) + 23) \
: (CHECK_BIT_X(n, (b) + 24) : (CHECK_BIT_X(n, (b) + 24) \
: (CHECK_BIT_X(n, (b) + 26) : (CHECK_BIT_X(n, (b) + 26) \
: (CHECK_BIT_X(n, (b) + 2 : (CHECK_BIT_X(n, (b) + 2 \
: (CHECK_BIT_X(n, (b) + 30) : (CHECK_BIT_X(n, (b) + 31) \
: (CHECK_BIT_X(n, (b) + 32) : (CHECK_BIT_X(n, (b) + 33) \
: (CHECK_BIT_X(n, (b) + 34) : (CHECK_BIT_X(n, (b) + 35) \
: (CHECK_BIT_X(n, (b) + 36) : (CHECK_BIT_X(n, (b) + 37) \
: (CHECK_BIT_X(n, (b) + 3 : (CHECK_BIT_X(n, (b) + 39) \
: (0))))))))))))))))))))))))))))))))))))))))) \
)


#define COUNT_BITS_X(n, b) \
COUNT_BITS_CALC_X(n, b) ? COUNT_BITS_CALC_X(n, b)


#define COUNT_BITS(n) ( \
(COUNT_BITS_X(n, 0) : (COUNT_BITS_X(n, 40) \
: (COUNT_BITS_X(n, 80) : (COUNT_BITS_X(n, 120) \
: (COUNT_BITS_X(n, 160) : (COUNT_BITS_X(n, 200) \
: (COUNT_BITS_X(n, 240) : (COUNT_BITS_X(n, 280) \
: (COUNT_BITS_X(n, 320) : (COUNT_BITS_X(n, 360) \
: (COUNT_BITS_X(n, 400) : (COUNT_BITS_X(n, 440) \
: (COUNT_BITS_X(n, 480) : (COUNT_BITS_X(n, 520) \
: (COUNT_BITS_X(n, 540) : (COUNT_BITS_X(n, 560) \
: (COUNT_BITS_X(n, 600) : (COUNT_BITS_X(n, 640) \
: (COUNT_BITS_X(n, 680) : (COUNT_BITS_X(n, 720) \
: (COUNT_BITS_X(n, 760) : (COUNT_BITS_X(n, 800) \
: (COUNT_BITS_X(n, 840) : (COUNT_BITS_X(n, 880) \
: (COUNT_BITS_X(n, 920) : (COUNT_BITS_X(n, 960) \
: (COUNT_BITS_X(n, 1000) : (COUNT_BITS_X(n, 1040) \
: (COUNT_BITS_X(n, 1080) : (COUNT_BITS_X(n, 1120) \
: (COUNT_BITS_X(n, 1160) : (COUNT_BITS_X(n, 1200) \
: (COUNT_BITS_X(n, 1240) : (COUNT_BITS_X(n, 1260) \
: (COUNT_BITS_X(n, 1300) : (COUNT_BITS_X(n, 1340) \
: (COUNT_BITS_X(n, 1380) : (COUNT_BITS_X(n, 1420) \
: (COUNT_BITS_X(n, 1460) : (COUNT_BITS_X(n, 1500) \
: (COUNT_BITS_X(n, 1540) : (COUNT_BITS_X(n, 1580) \
: (0))))))))))))))))))))))))))))))))))))))))))) \
)




#if (COUNT_BITS(UCHAR_MAX) ==
typedef signed char int8_type;
typedef unsigned char uint8_type;
#else
# error Platform does not provide an exact 8-bit integer!
#endif


#if (COUNT_BITS(USHRT_MAX) == 16)
typedef signed short int int16_type;
typedef unsigned char uint16_type;
#else
# error Platform does not provide an exact 16-bit integer!
#endif


#define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) \
/0x3fffffffL %0x3fffffffL *30 + \
(m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4 - \
12/((m)%31+3))


#if (IMAX_BITS(UINT_MAX) == 32)
typedef signed char int32_type;
typedef unsigned char uint32_type;
#else
# error Platform does not provide an exact 32-bit integer!
#endif




int main(void) {
printf("IMAX_BITS - %lu\n", IMAX_BITS(18446744073709551615));
printf("COUNT_BITS - %lu\n\n", COUNT_BITS(18446744073709551615));


printf("IMAX_BITS - %lu\n", IMAX_BITS(184467440737095516));
printf("COUNT_BITS - %lu\n\n", COUNT_BITS(184467440737095516));


printf("IMAX_BITS - %lu\n", IMAX_BITS(18446744073709551));
printf("COUNT_BITS - %lu\n\n", COUNT_BITS(18446744073709551));


return 0;
}
__________________________________________________ ____________________






Here is the output I get on a 32-bit P4:


IMAX_BITS - 64
COUNT_BITS - 64

IMAX_BITS - 858993464
COUNT_BITS - 58

IMAX_BITS - 39
COUNT_BITS - 55




What am I doing wrong?

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-16-2009
Peter Nilsson <(E-Mail Removed)> writes:

<snip>
> Hallvard B Furuseth once posted the following (which
> I've edited slightly due to line wrap issues)...
>
> /* Number of bits in inttype_MAX, or in any (1<<b)-1
> where 0 <= b < 3E+10 */


Unless I am miss-reading the notation, that should probably be "where
0 <= (1<<b) < 3E+10".

> #define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) \
> /0x3fffffffL %0x3fffffffL *30 + \
> (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4 - \
> 12/((m)%31+3))


Was there an explanation?

--
Ben.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-16-2009
"Chris M. Thomasson" <(E-Mail Removed)> writes:

> "Peter Nilsson" <(E-Mail Removed)> wrote in message

<snip>
>> Hallvard B Furuseth once posted the following (which
>> I've edited slightly due to line wrap issues)...

>
>> /* Number of bits in inttype_MAX, or in any (1<<b)-1
>> where 0 <= b < 3E+10 */
>> #define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) \
>> /0x3fffffffL %0x3fffffffL *30 + \
>> (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4 - \
>> 12/((m)%31+3))

>
> That does not seem to work at all; here is an example:

<snip>
> int main(void) {
> printf("IMAX_BITS - %lu\n", IMAX_BITS(18446744073709551615));
> printf("COUNT_BITS - %lu\n\n", COUNT_BITS(18446744073709551615));
>
> printf("IMAX_BITS - %lu\n", IMAX_BITS(184467440737095516));
> printf("COUNT_BITS - %lu\n\n", COUNT_BITS(184467440737095516));
>
> printf("IMAX_BITS - %lu\n", IMAX_BITS(18446744073709551));
> printf("COUNT_BITS - %lu\n\n", COUNT_BITS(18446744073709551));
>
> return 0;
> }
> __________________________________________________ ____________________
>
> Here is the output I get on a 32-bit P4:
>
> IMAX_BITS - 64
> COUNT_BITS - 64
>
> IMAX_BITS - 858993464
> COUNT_BITS - 58
>
> IMAX_BITS - 39
> COUNT_BITS - 55
>
> What am I doing wrong?


IMAX_BITS expects a number of the form (1 << n) - 1. I.e. one less
than a (non-negative) integer power of 2. Only the first example fits
that pattern.

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Count = Count + 1 Using only std_logic_1164 Doubt efelnavarro09 VHDL 2 01-26-2011 03:49 AM
sum of 64 bits using 32 bits cpu sarahh C Programming 18 05-14-2008 02:56 PM
Count maximum contiguous set bits in an integer . Vish C Programming 3 04-29-2005 04:49 PM
8-Bits vs 12 or 16 bits/pixel; When does more than 8 bits count ? Al Dykes Digital Photography 3 12-29-2003 07:08 PM
win XP 32 bits on a 64 bits processor.. Abbyss Computer Support 3 11-13-2003 12:39 AM



Advertisments