|
続きです
------- Dan Pop↓ -------------------------------------------------------
In Emmanuel Delahaye writes:
>In 'comp.lang.c', "John" wrote:
>> int bits = sizeof(long) * CHAR_BIT; /* 32 on my system */
>>
>> Is this a portable way to find out how many bits are in a given type?
>No. Only a char is both defined in number of bits and by a range.
Nope, it is unsigned char that has this property. Plain char may have
padding bits, like any other type.
うんにゃ。この属性をもつのは unsigned char です。'単なるchar'は他のすべての型
と同じく、パディングビットをもつかもしれません。
------- Dan Pop↓ ------------------------------------------------------------
In rjh writes:
>John wrote:
>> int bits = sizeof(long) * CHAR_BIT; /* 32 on my system */
>>
>> Is this a portable way to find out how many bits are in a given type?
>Not quite. :-(
>size_t bits = sizeof(long) * CHAR_BIT is better,
Or worse, depending on the relationship between INT_MAX and (size_t)-1 :-)
In practice, neither is better or worse than the other.
より悪いともいえます。INT_MAX と (size_t)-1 の関係次第では。:-)
実際には、どちらがどちらより良くも悪くもありません。
------- Dan Pop↓ ----------------------------------------------------------
In "John" writes:
>int bits = sizeof(long) * CHAR_BIT; /* 32 on my system */
>Is this a portable way to find out how many bits are in a given type?
It is a portable method of obtaining the size in bits of a type.
その方法は型のビット単位のサイズを得るための可搬性のある方法です。
However, this value is not particularly useful, because you have no
guarantee that all the bits contribute to the actual value (well, in
your particular example you have, because a long can't have less than
31 value bits and one sign bit).
しかし、その値は特に役に立つ値ではありません。なぜなら、そのすべての
ビットが本当の値に対して貢献しているという保証はないからです(ところで、
あなたが示した特別な例ではこの保証があります。なぜならlong型は31ビット
と符号の1ビットより少ないビット数を持つことはありえないからです)。
Besides value bits and sign bit, you can also have padding bits, which
you can count, but you cannot directly access (you must alias your long
with an array of unsigned char to get at them). You can figure out the
number of padding bits by computing the number of value bits (from
LONG_MAX, defined in ). Just subtract this number and one
for the sign bit from sizeof(long) * CHAR_BIT.
値のビットと符号ビットに加えて、パディングビットがあるかもしれません。そ
のパディングビットの数を数えることはできますが、それに直接アクセスするこ
とは出来ません(アクセスする為には long を unsigned char の配列でaliasしな
ければならない)。あなたは値のビット数を(LONG_MAXを使って)計算することによ
ってパディングビットの数を求めることが出来ます。値のビット数と符号ビット
分の1を sizeof(long) * CHAR_BIT から引くだけです。
Note that, in practice, hosted implementations using padding bits
are few and far between.
実際のところ、パディングビットを使っているホスト処理系はわずだという
ことに注意してください。
|