C ++标准是否要求有符号整数只有一个符号位? [英] Does the C++ standard requires signed integers to have exactly one sign bit?

查看:192
本文介绍了C ++标准是否要求有符号整数只有一个符号位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑C ++的基本有符号整数类型,即: signed char short int int long int long long int ,当前的C ++标准是什么需要关于它们的基础位表示?

Consider the fundamental signed integer types of C++, namely: signed char, short int, int, long int and long long int, what does the current C++ standard require about their underlying bit representation?

它们的位表示的约束是否指定它们应该包括:

Does the constraints on their bit representation specify that they should include:


  • 可选填充位

  • 强制值位

  • 强制符号位 0 表示正值, 1 表示负值

  • 如果存在,则符号位应该是最重要的位

  • optional padding bits
  • mandatory value bits
  • a mandatory sign bit that is 0 for positive values, and 1 for negative value
  • if it exists, the sign bit should be the most significant bit

这是真的吗?如果没有,那么什么是重建者呢?我正在寻找证明或反驳此标准的报价。

Is this true? If not, then what are the constaints? I am searching for quotes from the standard that proves or disproves this.

编辑:我问这个问题,因为,在C中,标准说:

I am asking this question, because, in C, the standard says:


6.2.6.2.2:

对于有符号整数类型,对象表示的位应将
分为三组:值位,填充位和符号
位。不需要任何填充位; signed char不应该有
任何填充位。应该只有一个符号位。
是值位的每个位应与相应无符号类型的对象
表示中的相同位具有相同的值(如果有符号类型中有M
值位且N在无符号类型中,则M≤N
)。如果符号位为零,则不会影响结果值。
如果符号位为1,则应以下列方式在
之一中修改该值:

For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. There need not be any padding bits; signed char shall not have any padding bits. There shall be exactly one sign bit. Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then M ≤ N ). If the sign bit is zero, it shall not affect the resulting value. If the sign bit is one, the value shall be modified in one of the following ways:


  • 符号位0的对应值被否定(符号和幅度);

  • 符号位的值为 - (2 ^ M)(二进制补码);

  • 符号位的值为 - (2 ^ M - 1)(1'实现)。

其中哪一个适用于实现定义,
是否为符号位1且所有值位为零(对于
前两个),或者是符号位和所有值位1(对于'
补码),是陷阱表示或正常值。如果
符号和幅度以及1'补码,如果这个表示是
a正常值,则称为负零。

Which of these applies is implementation-defined, as is whether the value with sign bit 1 and all value bits zero (for the first two), or with sign bit and all value bits 1 (for ones’ complement), is a trap representation or a normal value. In the case of sign and magnitude and ones’ complement, if this representation is a normal value it is called a negative zero.

所以我想知道C ++中是否存在类似的东西

So I am wondering whether something comparable exists in C++

推荐答案

这就是C ++ 11所说的有符号整数类型的表示:

This is what C++11 says about representation of signed integer types:

C ++ 11 N3337 3.9.1 [basic.fundamental] P7:

C++11 N3337 3.9.1 [basic.fundamental] P7:


整数类型
的表示应使用纯二进制计算系统定义值。 49 [例子:本国际标准允许积分类型的2的补码,1的补码和带符号的幅度表示。 - 结束
示例]

The representations of integral types shall define values by use of a pure binary numeration system. 49 [ Example: this International Standard permits 2’s complement, 1’s complement and signed magnitude representations for integral types. — end example ]

其中脚注49为:


49)使用二进制数字0和1的整数的位置表示,其中由连续位表示的值是加法的,以1开始,并乘以2的连续积分幂,除了或许对于具有最高
位置的位。 (改编自美国国家信息处理系统词典。)

49) A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position. (Adapted from the American National Dictionary for Information Processing Systems.)

因此C ++允许与C相同的三个选项,以及任何东西其他令人满意的脚注49.哪个是C允许的超集。但是,在脚注49中,只允许最高位具有特殊含义。

Thus C++ allows the same three options as C, as well as anything else satisfying Footnote 49. Which is a superset of what C allows. By Footnote 49, however, only the highest bit is allowed to have special meaning.

这篇关于C ++标准是否要求有符号整数只有一个符号位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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