Re: #define BITS_PER(x) (8 * sizeof(x))
- From: Flash Gordon <smap@xxxxxxxxxxxxxxxxx>
- Date: Sat, 23 May 2009 09:30:00 +0100
James Dow Allen wrote:
Keith Thompson <kst-u@xxxxxxx> might have writ, in news:lnmy956lqy.fsf@xxxxxxxxxxxxxxx:
James Dow Allen <jdallen2000@xxxxxxxxx> writes:Summary.Sorry, I must have missed something. I'd definitely use CHAR_BIT, not
I will continue to use something like
#define BITS_PER(x) (8*sizeof(x))
If I get paranoid I'll call a test routine early in
main(), something like
8, for two reasons. First, and perhaps *less* important, CHAR_BIT
isn't necessarily 8 on all systems (I say this may be less important
because I've never programmed for a system with CHAR_BIT != 8).
Second, it's clearer to use a symbolic name rather than a magic
number.
Why do you think 8 might be better than CHAR_BIT?
No one has, as yet in this thread, presented a specific system where
char_bit != 8,
Plenty have been presented in the past. It is actually quite common for certain types of processors, such as DSPs.
nor where integers have padding
That would be more unusual.
BUT an unchallenged inference from Twirlip's comments suggest that this (hypothetical?) machine has 9-bit chars and 32-bit integers.
People are allowed to present hypothetical machines to make a point. I assume that is 32 value bits with 4 padding bits.
*Then* 8*sizeof would
work but char_bit*sizeof *not* work.
That would be far more unusual that the situation where CHAR_BIT*sizeof worked but 8*sizeof was wrong.
Another reason to suspect this is the correct approach is that
GMP uses 8, not char_bit. GMP is a very thoroughly tested and
ported piece of code.
That does *not* mean it is maximally portable. For a start, it's main targets (according to its web page) are Unix like systems (and POSIX requires CHAR_BIT==8) with it also having been ported to Windows. So they do not claim it is portable to machines where CHAR_BIT!=8.
Finally, if ints DO have 36 bits but code assumes 32 bits, the
code (if coded with care) will still work, but the converse
obviously doesn't apply.
True.
The *real* solution is for the "standard header(s)" to contain
a BITS_PER_INT define and my question remains: Why don't they?
(Committee oversight is not an adequate answer; if the standard
needs to evolve, let it evolve.)
Put in a request for it to be added to the standard. You could start by asking on comp.std.c where people on the standards committee are more likely to see the question.
--
Flash Gordon
.
- References:
- When shorts are longer than longs !
- From: James Dow Allen
- Re: When shorts are longer than longs !
- From: Richard Heathfield
- Re: When shorts are longer than longs !
- From: James Dow Allen
- Re: When shorts are longer than longs !
- From: Richard Heathfield
- Re: When shorts are longer than longs !
- From: James Dow Allen
- Re: When shorts are longer than longs !
- From: Richard Heathfield
- Implementation of getbits() / setbits()
- From: James Dow Allen
- Re: Implementation of getbits() / setbits()
- From: Twirlip of the Mists
- #define BITS_PER(x) (8 * sizeof(x))
- From: James Dow Allen
- Re: #define BITS_PER(x) (8 * sizeof(x))
- From: Keith Thompson
- Re: #define BITS_PER(x) (8 * sizeof(x))
- From: James Dow Allen
- When shorts are longer than longs !
- Prev by Date: ??? China Wholesaler- Brand Shoes, NIKE,SHOX,ADIDAS,AIR MAX,etc.--(www.mall0086.com)
- Next by Date: Re: comma operator
- Previous by thread: Re: #define BITS_PER(x) (8 * sizeof(x))
- Next by thread: Re: #define BITS_PER(x) (8 * sizeof(x))
- Index(es):
Relevant Pages
|