C++ 根据标准,整数中的值表示位数?

C++ 根据标准,整数中的值表示位数?,c++,standards,language-lawyer,c++14,bit,C++,Standards,Language Lawyer,C++14,Bit,考虑以下帮助器结构: template <class T> struct bit_count_1: std::integral_constant< std::size_t, std::numeric_limits<typename std::make_unsigned<T>::type>::digits > {}; template <class T> struct bit_count_2: std::integral

考虑以下帮助器结构:

template <class T>
struct bit_count_1:
std::integral_constant<
    std::size_t,
    std::numeric_limits<typename std::make_unsigned<T>::type>::digits
> {};

template <class T>
struct bit_count_2:
std::integral_constant<
    std::size_t,
    std::numeric_limits<T>::digits + std::is_signed<T>::value
> {};

template <class T>
constexpr std::size_t compute_bit_count() {
    using type = typename std::make_unsigned<T>::type;
    constexpr type zero = 0;
    constexpr type one = 1;
    constexpr type max = ~zero;
    type current = max;
    std::size_t i = 0; 
    while (current) {
        current >>= one;
        ++i;
    }
    return i;
}

template <class T>
struct bit_count_3:
std::integral_constant<
    std::size_t,
    compute_bit_count<T>()
> {};
模板
结构位\u计数\u 1:
积分常数<
标准:尺寸,
标准::数字限制::数字
> {};
模板
结构位\u计数\u 2:
积分常数<
标准:尺寸,
标准::数字限制::数字+标准::有符号::值
> {};
模板
constexpr std::size\u t compute\u bit\u count(){
使用type=typename std::make_unsigned::type;
constexpr类型0=0;
constexpr类型1=1;
constexpr type max=~0;
类型电流=最大值;
标准:尺寸i=0;
while(当前){
当前>>=一个;
++一,;
}
返回i;
}
模板
结构位\u计数\u 3:
积分常数<
标准:尺寸,
计算位计数()
> {};
对于每种积分类型
T
std::is_integral::value
is
true
除了
bool
之外,我是否根据标准保证:

  • 位计数1
    位计数2
    位计数3
    具有相同的值
    N
  • tx=1;x=(N-1)
    定义良好

我现在正在研究一个C++提案,所以我需要根据标准来确定这是否是真的,目前我还不清楚。

<当然,当然,你已经知道了。p> [basic.types]3.9\4

对象的值表示是一组保持的位 类型T的值

[basic.basical]3.9.1\3

有符号整数类型的非负值范围为 相应无符号整数类型的子范围,以及值 每个相应的有符号/无符号类型的表示应为 相同

[basic.basical]3.9.1\7

积分类型的表示应通过使用 纯二进制记数系统.50

50)使用 二进制数字0和1,其中值由连续的 位是加法的,从1开始,然后乘以连续的 整数幂为2,除了最高 位置。(改编自《美国国家英语词典》 信息处理系统。)

但这取决于什么是非符号位:

[numeric.limits.members]18.3.2.4\9

对于整数类型,表示中的非符号位的数量

如果它们暗示,在所谓的表达式中,符号位必须仅为,则这些表达式的计算结果应为
true

  • bit\u count\u 2{}>bit\u count\u 1{}
    ,以及
  • bit\u count\u 2{}>bit\u count\u 3{}
对于用2或1的补码表示的有符号整数类型T。
所以,我会放一个
static\u assert
无论如何,你知道……

你可能想交换这个问题上的一个标签,以帮助吸引合适的观众……还有一个标签是普通的“C++”标签,后面的标签远远超过C++11和C++14。我要么放弃,要么放弃,但我会把这个留给你。