值位与对象位 [英] Value Bits Vs Object Bits

查看:64
本文介绍了值位与对象位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



内存中无符号整数类型使用的位数可以通过以下方式确定:
typedef unsigned long long UIntType;


CHAR_BIT * sizeof(UIntType)

然而,什么是一个很好的可移植方式来确定这些

位中有多少价值比特?如果可能的话,将它作为一个

编译时间常量会很好。

这是我玩过的东西:

typedef unsigned long long UIntType;

unsigned GetQuantityValueBits(void)

{

/ *我们知道它至少是8个* /


未签名数量= 8;


UIntType val = 128;


而(val< ;< = 1)++数量;


返回数量;

}

另外,我们可以确定金额来自的非值位(陷阱位

):


sizeof(UIntType)* CHAR_BIT - GetQuantityValueBits()

- Tomás


The quantity of bits used by an unsigned integer type in memory can be
determined by:
typedef unsigned long long UIntType;

CHAR_BIT * sizeof(UIntType)
However, what would be a good portable way to determine how many of these
bits are value bits? If possible, it would be nice to have it as a
compile time constant.
Here''s something I played around with:
typedef unsigned long long UIntType;
unsigned GetQuantityValueBits(void)
{
/* We know it''s atleast 8 in anyway */

unsigned quantity = 8;

UIntType val = 128;

while (val <<= 1) ++quantity;

return quantity;
}
Also, we could determine the amount of non-value bits (trapping bits
perhaps) from:

sizeof(UIntType) * CHAR_BIT - GetQuantityValueBits()
-Tomás

推荐答案

Tomás写道:
Tomás wrote:
无符号整数类型在内存中使用的位数可以由
决定:

typedef unsigned long long UIntType;

CHAR_BIT * sizeof(UIntType)

但是,什么将是一个很好的可移植方式来确定这些位中有多少是值位?如果可能的话,将它作为编译时间常量会很好。
The quantity of bits used by an unsigned integer type in memory can be
determined by:
typedef unsigned long long UIntType;

CHAR_BIT * sizeof(UIntType)
However, what would be a good portable way to determine how many of these
bits are value bits? If possible, it would be nice to have it as a
compile time constant.




为自己说话,我从来没有发现过

为此写一个常量表达式的方法。但是,它很容易在运行时通过从值为
的所有值开始并计算需要多少一位移位来确定运行时间 b $ b在值变为零之前,
。你也可以尝试一下

就像ceil(log((UIntType)-1)/ log(2))。


这个信息偶尔会让人讨厌是不是很容易获得
。对于许多用途,上限是所需的所有
和CHAR_BIT * sizeof(UIntType)足够好。对于

不同类别的使用,需要范围界限,并且你可以从(UIntType)-1获得
。但对于管理

" arrays"将单位标志打包成更大的单位,这是令人讨厌的,在编译时不能计算值位数。


忏悔:我通常忽略PB的可能性和

只是假装CHAR_BIT * sizeof是正确答案。鼻子

恶魔还没有惩罚我(无论如何都是为了这个罪),但是

当然标准不能保证时效性

报复。


-

Eric Sosman
es ***** @ acm-dot-org.inva lid



Speaking for myself, I''ve never been able to discover a
way to write a constant expression for this. However, it''s
easy enough to determine at run-time by starting with a value
of all-ones and counting how many one-bit shifts are required
before the value becomes zero. You could also try something
like ceil(log((UIntType)-1)/log(2)).

It is occasionally annoying that this information is not
easily available. For many uses an upper bound is all that''s
needed and CHAR_BIT*sizeof(UIntType) is good enough. For a
different class of uses a range bound is wanted, and you can
get that from (UIntType)-1. But for things like managing
"arrays" of single-bit flags packed into larger units, it is
annoying that the value bits can''t be counted at compile time.

Confession: I usually ignore the possibility of P.B.s and
just pretend CHAR_BIT*sizeof is the Right Answer. The nasal
demons haven''t punished me yet (for that sin, anyway), but of
course the Standard makes no guarantee about the timeliness
of retribution.

--
Eric Sosman
es*****@acm-dot-org.invalid


Eric Sosman写道:
Eric Sosman writes:
Tomás写道:
Tomás wrote:
无符号整数类型在内存中使用的位数可以通过以下方式确定:
typedef unsigned long long UIntType;
CHAR_BIT * sizeof(UIntType)
然而,什么是一个很好的可移植方式来确定这些位中有多少是值位?如果可能的话,将它作为编译时间常量会很好。
对于我自己来说,我从来没有能够发现为此编写常量表达式的方法。 。
The quantity of bits used by an unsigned integer type in memory can be
determined by:
typedef unsigned long long UIntType;
CHAR_BIT * sizeof(UIntType)
However, what would be a good portable way to determine how many of
these bits are value bits? If possible, it would be nice to have it as
a compile time constant.
Speaking for myself, I''ve never been able to discover a
way to write a constant expression for this.




我有。之前发布它。


/ * inttype_MAX中的位数,或任何(1 << k)-1中的位数,其中0 <= k <1。 3.2E + 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))


所以IMAX_BITS((unsigned_type)-1)计算

无符号整数类型中的位数。 IMAX_BITS(INT_MAX)计算int中的位数

。直到某人实现4千兆字节的整数类型:-)


或者如果你对UINTMAX_MAX的大小不太偏执:


/ * inttype_MAX或任何(1 <<< k)-1中的位数,其中0 <= k <1。 2040 * /

#define IMAX_BITS(m)((m)/((m)%255 + 1)/ 255%255 * 8 + 7-86 /((m)%255 + 12 ))

忏悔:我通常会忽略PB的可能性,而
只是假装CHAR_BIT * sizeof是正确答案。鼻子恶魔还没有惩罚我(无论如何都是为了这个罪),但是当然,标准并不能保证报复的及时性。



I have. Posted it before.

/* Number of bits in inttype_MAX, or in any (1<<k)-1 where 0 <= k < 3.2E+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))

So IMAX_BITS((unsigned_type)-1) computes the number of bits in an
unsigned integer type. IMAX_BITS(INT_MAX) computes the number of bits
in an int. Until someone implements a 4-gigabyte integer type:-)

Or if you are less paranoid about how large UINTMAX_MAX can get:

/* Number of bits in inttype_MAX, or in any (1<<k)-1 where 0 <= k < 2040 */
#define IMAX_BITS(m) ((m)/((m)%255+1) / 255%255*8 + 7-86/((m)%255+12))
Confession: I usually ignore the possibility of P.B.s and
just pretend CHAR_BIT*sizeof is the Right Answer. The nasal
demons haven''t punished me yet (for that sin, anyway), but of
course the Standard makes no guarantee about the timeliness
of retribution.




只要在错误时浪费了一点内存,那也是我的

偏好。 IMAX_BITS是一个有趣的小黑客,但不完全是

可读。


解释,''x ** y''意味着x提升到了y:

第1行计算(整个30位块的数量)* 30:

对于m =(2 **(K * n + r)) - 1和P =(2 ** K)-1,K = 30,P = 0x3fffffff,

m =(P + 1)** n * 2 ** r - 1,

m%P + 1 = 1 * 2 ** r - 1 + 1 = 2 ** r当2 ** r-1
m /(m%P +1)=(P + 1)** n - 1

=((P + 1)-1)*((P + 1)** 0 + ... +(P + 1)**(n-1)),

... / P%P * K =(1 ** 0 + ... + 1 **(n-1))%P *当n
= n * K P.

第2部分与剩余部分(m%0x3fffffff)相同,K = 5,P = 31。

第3部分发生计算最终0-4位m%31 = [0/1/3/7/15]。

m%31是m%0x3fffffff%31的缩写,因为0x3fffffff%31 = 0 。

0x3fffffffL是最大的便携式2 ** x-1,具有这样的2 ** y-1因子。


-

Hallvard



As long as that just wastes a bit of memory when it''s wrong, that''s my
preference too. IMAX_BITS is a fun little hack, but not exactly
readable.

Explanation, were ''x**y'' means x raised to the power of y:
Line 1 computes (number of whole 30-bit chunks) * 30:
For m = (2**(K*n+r))-1 and P = (2**K)-1 with K=30, P=0x3fffffff,
m = (P+1)**n * 2**r - 1,
m % P + 1 = 1 * 2**r - 1 + 1 = 2**r when 2**r-1<P so r<K,
m /(m%P+1) = (P+1)**n - 1
= ((P+1)-1) * ((P+1)**0 +...+ (P+1)**(n-1)),
.../P%P*K = ( 1**0 +...+ 1**(n-1)) % P * K
= n*K when n < P.
Part 2 does the same to the remainder (m%0x3fffffff) with K=5, P=31.
Part 3 "happens" to count the final 0-4 bits in m%31=[0/1/3/7/15].
m % 31 is short for m % 0x3fffffff % 31, because 0x3fffffff % 31 = 0.
0x3fffffffL is the largest portable 2**x-1 with such a 2**y-1 factor.

--
Hallvard


Hallvard B Furuseth写道:
Hallvard B Furuseth wrote:
/ * inttype_MAX或任何(1< 1)中的位数。 < k)-1,其中0 <= k <1。 3.2E + 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))

所以IMAX_BITS((unsigned_type)-1)计算的数量
无符号整数类型中的位。
/* Number of bits in inttype_MAX, or in any (1<<k)-1 where 0 <= k < 3.2E+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))

So IMAX_BITS((unsigned_type)-1) computes the number of bits in an
unsigned integer type.




干得好。谢谢!


-

Thad



Great job. Thanks!

--
Thad


这篇关于值位与对象位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆