值位作为编译时常量! [英] Value bits as compile-time constant!
问题描述
在comp.lang.c上,Hallvard B Furuseth设计了一个编译时常量
,表示无符号
整数类型中的值表示位数。在真正的C方式中,这里它是一个宏:
/ * 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))
你为它提供一个无符号整数值,其位模式包含所有1'的
,例如:
1(十进制:1)
11(十进制:3)
111(十进制:7)
11111111(十进制:255)
要找出无符号整数类型中的值位数,你需要为它提供该类型的最大值(b $ b)即所有1'的位模式。获得无符号整数类型最大值的最简单方法是
为它分配-1:
unsigned const max_value = -1;
unsigned const amount_value_bits = IMAX_BITS(max_value);
这里是我写的一些模板代码,它使用了它:
模板< class T>
struct IMaxBits {
模板< T m>
struct AllOnes {
static unsigned const val =
m /(m%0x3fffffffL + 1)/ 0x3fffffffL%0x3fffffffL * 30
+ m%0x3fffffffL /(m%31 + 1)/ 31%31 * 5 + 4-12 /(m%31 + 3);
};
};
模板< class T>
struct AmountValueBits {
static unsigned const val = IMaxBits< T> ::模板AllOnes< -1> :: val;
};
#include< cstdlib>
#include< cstring>
#include< cstddef>
#include< cassert>
模板< std :: size_t width>
inline const char * CenterHoriz(const char * const p)
{
使用std :: memset;
使用std :: strlen;
使用std :: size_t;
static char spaces [width + 1];
memset(spaces,'''',width);
size_t const len = strlen(p);
断言(宽度> = len);
char * const pos = spaces +(width / 2 - len / 2);
memcpy(pos,p,len);
返回空格;
}
# include< iostream>
extern char const str_uchar [] =" unsigned char" ;;
extern char const str_ushort [] =" unsigned short" ;
extern char const str_uint [] =" unsigned int";
extern char const str_ulong [] =" unsigned long";
模板< class T,const char * str>
void PrintRow()
{
usi ng std :: cout;
unsigned const total_bits = sizeof(T)* AmountValueBits< unsigned
char> :: val;
unsigned const val_bits = AmountValueBits< T> :: val;
unsigned const pad_bits = total_bits - val_bits;
const char * const spaces_val =
val_bits> 99? "" :(val_bits> 9?"":"");
const char * const spaces_pad =
pad_bits> 99? "" :(pad_bits> 9?"":"");
const char * const spaces_total =
total_bits> 99? "" :(total_bits> 9?"":"");
cout<< " ||" << CenterHoriz< 21>(str)<< " ||
<< spaces_val<< val_bits<< " || " << spaces_pad<<
pad_bits
<< " || " << spaces_total<< total_bits<< " || \ n"
" ------------------------------ ---------------------------------------
\ n" ;;
}
int main()
{
使用std :: cout;
cout<<
"
=========== =================================== \ n"
" ||值位||填充位||总比特
|| \ n"
" ==================== ============================= ====================
\ n" ;;
PrintRow< unsigned char,str_uchar>();
PrintRow< unsigned short,str_ushort>( );
PrintRow< unsigned,str_uint>();
PrintRow< unsigned long,str_ulong>();
cout << \ n \ n \\ nn \\ n;
std :: system(" PAUSE");
}
我有兴趣听听是否有人得到一些有趣的信息。价值。
-
Frederick Gotham
Over on comp.lang.c, Hallvard B Furuseth devised a compile-time constant
representing the amount of value representation bits in an unsigned
integer type. In true C fashion, here it is as a macro:
/* 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))
You supply it with an unsigned integer value whose bit-pattern consists
of all 1''s, e.g.:
1 (Decimal: 1)
11 (Decimal: 3)
111 (Decimal: 7)
11111111 (Decimal: 255)
To find out the amount of value bits in an unsigned integer type, you
supply it with that type''s max value (i.e. a bit pattern of all 1''s). The
handiest way to get the max value of an unsigned integer type is to
assign -1 to it:
unsigned const max_value = -1;
unsigned const amount_value_bits = IMAX_BITS( max_value );
Here''s some template code I''ve written which makes use of it:
template<class T>
struct IMaxBits {
template<T m>
struct AllOnes {
static unsigned const val =
m /(m%0x3fffffffL+1) /0x3fffffffL %0x3fffffffL *30
+ m%0x3fffffffL /(m%31+1)/31%31*5 + 4-12/(m%31+3);
};
};
template<class T>
struct AmountValueBits {
static unsigned const val = IMaxBits<T>::template AllOnes<-1>::val;
};
#include <cstdlib>
#include <cstring>
#include <cstddef>
#include <cassert>
template<std::size_t width>
inline const char* CenterHoriz( const char * const p )
{
using std::memset;
using std::strlen;
using std::size_t;
static char spaces[width + 1];
memset( spaces, '' '', width );
size_t const len = strlen( p );
assert( width >= len );
char * const pos = spaces + ( width / 2 - len / 2 );
memcpy( pos, p, len );
return spaces;
}
#include <iostream>
extern char const str_uchar[] = "unsigned char";
extern char const str_ushort[] = "unsigned short";
extern char const str_uint[] = "unsigned int";
extern char const str_ulong[] = "unsigned long";
template<class T, const char* str>
void PrintRow()
{
using std::cout;
unsigned const total_bits = sizeof(T) * AmountValueBits<unsigned
char>::val;
unsigned const val_bits = AmountValueBits<T>::val;
unsigned const pad_bits = total_bits - val_bits;
const char * const spaces_val =
val_bits > 99 ? "" : ( val_bits > 9 ? " " : " " );
const char * const spaces_pad =
pad_bits > 99 ? "" : ( pad_bits > 9 ? " " : " " );
const char * const spaces_total =
total_bits > 99 ? "" : ( total_bits > 9 ? " " : " " );
cout << "||" << CenterHoriz<21>(str) << "|| "
<< spaces_val << val_bits << " || " << spaces_pad <<
pad_bits
<< " || " << spaces_total << total_bits << " ||\n"
"---------------------------------------------------------------------
\n";
}
int main()
{
using std::cout;
cout <<
"
==============================================\n"
" || Value bits || Padding Bits || Total Bits
||\n"
"================================================= ====================
\n";
PrintRow<unsigned char, str_uchar>();
PrintRow<unsigned short, str_ushort>();
PrintRow<unsigned, str_uint>();
PrintRow<unsigned long, str_ulong>();
cout << "\n\n\n";
std::system( "PAUSE" );
}
I''d be interested to hear if anyone gets some "interesting" values.
--
Frederick Gotham
推荐答案
* Frederick Gotham:
* Frederick Gotham:
在comp.lang.c上,Hallvard B Furuseth设计了一个编译时常量
,表示无符号整数类型中值表示位的数量。在真正的C方式中,这里它是一个宏:
/ * 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))
Over on comp.lang.c, Hallvard B Furuseth devised a compile-time constant
representing the amount of value representation bits in an unsigned
integer type. In true C fashion, here it is as a macro:
/* 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))
在C ++中你会使用std :: numeric_limits :: digits。
-
A:因为它搞砸了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门帖子。
Q :usenet和电子邮件中最烦人的事情是什么?
In C++ you''d use std::numeric_limits::digits instead.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
" Frederick Gotham" < FG ******* @ SPAM.com>在消息中写道
news:Xn *************************** @ 194.125.133.14。 ..
"Frederick Gotham" <fg*******@SPAM.com> wrote in message
news:Xn***************************@194.125.133.14. ..
在comp.lang.c上,Hallvard B Furuseth设计了一个编译时常量
,表示无符号整数类型中的值表示位数。在真正的C方式中,这里是一个宏:
Over on comp.lang.c, Hallvard B Furuseth devised a compile-time constant
representing the amount of value representation bits in an unsigned
integer type. In true C fashion, here it is as a macro:
这是什么意思:值表示位的数量?
>
-Howard
What does this mean: "the amount of value representation bits"?
-Howard
Howard写道:
" Frederick Gotham" < FG ******* @ SPAM.com>在消息中写道
新闻:Xn *************************** @ 194.125.133.14。 ..
"Frederick Gotham" <fg*******@SPAM.com> wrote in message
news:Xn***************************@194.125.133.14. ..
在comp.lang.c上,Hallvard B Furuseth设计了一个编译时
常量,表示无符号整数类型中值表示位的数量。在真正的C方式中,这里是一个宏:
Over on comp.lang.c, Hallvard B Furuseth devised a compile-time
constant representing the amount of value representation bits in an
unsigned integer type. In true C fashion, here it is as a macro:
这意味着什么:值表示位的数量?
What does this mean: "the amount of value representation bits"?
>
用''数字'代替''金额'',它是否更简单?如果没有,
substitue''value representation''with''value-represent''。如果
之后还不清楚,那么定义可能是代表值所涉及的
位的数量。
V
-
请在通过电子邮件回复时删除资金''A'
我这样做没有回复最热门的回复,请不要问
Substitute ''amount'' with ''number'', does it make it simpler? If not,
substitue ''value representation'' with ''value-representing''. If after
that it''s not clear, then the definition is probably "the number of
bits involved in representing the value".
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
这篇关于值位作为编译时常量!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!