标准的一部分是整数位表示形式的哪一部分? [英] What part of integer bit-representation is part of The Standard?

查看:105
本文介绍了标准的一部分是整数位表示形式的哪一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于整数在内存中的表示,可以假设多少?

将整数切片和拼接在一起的可移植方式是按位操作的吗?

解决方案

C标准在6.2.6.2节中讨论了整数类型的表示形式.

它指定整数类型的二进制表示形式.对于无符号类型,这些位分为值位填充位.填充位对值无贡献;不需要任何填充位.对于带符号的类型,只有一个符号位.可以使用符号和大小二进制补码一个补码来表示带符号的类型(二进制补码在现代系统中几乎是通用的)./p>

位的顺序以及填充位的存在和数量是实现定义的. (大多数现代实现没有填充位.)

在C99中引入了填充位的概念以及对三个规范表示的限制.

按位运算符(<<>>&等)是根据构成操作数值表示形式的位来定义的,但是对表示形式的要求足够具体,以至于在大多数情况下,这是明确的.对<<>>移位运算符的描述具体说,例如,E1 << E2的结果为E1×2 E2 ;参见引用的N1570草案的6.5.7节.

C ++标准有一个非规范性注释,它表示允许整数类型使用2的补码,1的补码和带符号的幅度表示,但似乎没有明确声明不允许使用其他表示.它确实要求整数类型的表示应通过使用纯二进制计算系统来定义值".您可以在 N4296工作草案中查看血腥细节. (或其他任何草案,如果有副本,也可以包含在标准本身中).

How much can one assume about integers' representation in memory?

How portable way of slicing and splicing integers together are bit-wise operations?

解决方案

The C standard discusses the representations of integer types in section 6.2.6.2.

It specifies a binary representation for integer types. For unsigned types, the bits are divided into value bits and padding bits. Padding bits do not contribute to the value; there needn't be any padding bits. For signed types, there is a single sign bit. Signed types may be represented using either sign and magnitude, two's complement, or one's complement (two's complement is nearly universal for modern systems).

The ordering of the bits, and the presence and number of padding bits, are implementation-defined. (Most modern implementations do not have padding bits).

The concept of padding bits, and the restriction to the three canonical representations, were introduced in C99.

The bitwise operators (<<, >>, &, et al) are defined in terms of the bits making up the representation of the values of the operands, but the requirements on the representations are specific enough that this is unambiguous for most cases. The description of the << and >> shift operators specifically says that, for example, the result of E1 << E2 is E1 × 2E2; see section 6.5.7 of the cited N1570 draft.

The C++ standard has a non-normative note that says it permits 2’s complement, 1’s complement and signed magnitude representations for integral types, but there doesn't seem to be an explicit statement that no other representations are permitted. It does require that "The representations of integral types shall define values by use of a pure binary numeration system.". You can see the gory details in the N4296 working draft of the C++ standard (or in any other draft, or in the standard itself if you have a copy).

这篇关于标准的一部分是整数位表示形式的哪一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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