将int的所有字节设置为(unsigned char)0,保证表示零? [英] Set all bytes of int to (unsigned char)0, guaranteed to represent zero?

查看:244
本文介绍了将int的所有字节设置为(unsigned char)0,保证表示零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ em> -standard实际上保证将整数类型的所有字节转换为(unsigned char)0 的值。







问题



下面的代码段是 true 的 if-语句 class =post-tagtitle =show questions tagged'c ++ 11' =tag> c ++ 11

  std :: memset(
reinterpret_cast< char *>(& a),// int a;
(unsigned char)0,
sizeof (int)
);

if(a == 0){
...
}

通过阅读C99和C ++ 11标准的引文(在本文的后面),我们发现C99明确保证一个整数类型,所有位设置为 0 将代表该类型中的值 0



我无法在C ++ 11标准中找到此保证。







< b






在C99(ISO / IEC 9899:1999)




5.2.1.2/1多字节字符



所有位为零的字节应解释为空字符
独立于移位状态。这样的字节不应作为任何
其他多字节字符的一部分出现。


p>


6.2.6.2/1整数类型



任何填充位的值未指定。 45)有符号整数类型的
(非陷阱)对象表示形式,其中
符号位为零是对应
的有效对象表示



对于任何整数
类型,其中所有位都为零的对象表示形式应为$










在C ++ 11中(ISO / IEC 14882:2011)




set [lex.charset]



基本执行字符集和基本执行
wide-character set每个都包含基本$ b $的所有成员b源字符集,加上表示警报,
退格和回车的控制字符,加上空字符(分别为
空宽字符),其表示所有零位



解决方案

C ++ 11



我认为相关的部分是



3.9.1 / 1在C ++ 11中


对于字符类型,对象表示的所有位都在值表示中参与
。对于无符号字符类型,值表示的所有可能的位模式表示数字。


与3.9.1 / 7一起


整数类型
的表示应使用纯二进制数字系统定义值。




C11



6.2.6.2非常明确


除了unsigned char之外的无符号整数类型,对象
表示的位应该被分为两组:值位和填充位(需要
不是后者的任何一个)。如果存在N个值比特,则每个比特将代表在1和2 N-1 之间的不同的
次幂2,使得该类型的对象能够表示使用纯二进制表示从0到2的值;这将是
被称为值表示。



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



- 符号位0的相应值被否定和幅度);



- 符号位具有值 - (2 M )(二进制补码);



- 符号位具有值 - (2 M-1 )(个数补码)。



这些应用中的哪一个是实现定义的,无论具有符号位1
的值和所有值位(对于前两个)为零,还是具有符号位和所有值位1(对于1的补码)是陷阱表示或正常值。在sign和
的情况下,如果这个表示是正常值,它被称为
负零。




Summmary



我认为这两种标准的目的是一样的。




  • char signed char unsigned char 有所有位参与值


  • 其他整数类型可能有填充位价值。


  • 解释是一个纯二进制表示,其定义在上述C11引文中扩展。




有两件可能不清楚的事情:




  • 可以-0(对于符号和大小和_ones'补语)是C ++中的陷阱值


>

我会保守,对两者都是肯定的。


This is not a matter of recommended practise (nor undefined behavior), but about what the c++-standard actually guarantees in the matter of turning all bytes of an integer type to the value of (unsigned char)0.


The Question(s)

In the snippet below, is the expression used by the if-statement guaranteed to be evaluated to true in ?

std::memset (
  reinterpret_cast<char*> (&a), // int a;
  (unsigned char)0,
  sizeof (int)
);

if (a == 0) {
  ...
}

By reading the quotations from the C99 and C++11 standard (further down in this post) we find that C99 explicitly guarantees that an integer type with all bits set to 0 will represent the value 0 in that type.

I cannot find this guarantee in the C++11 standard.

  • Is there no such guarantee?
  • Is the result of the previous snippet really implementation-specific?


In C99 (ISO/IEC 9899:1999)

5.2.1.2/1 Multibyte characters

A byte with all bits zero shall be interpreted as a null character independent of shift state. Such a byte shall not occur as part of any other multibyte character.

6.2.6.2/1 Integer types

The values of any padding bits are unspecified.45) A valid (non-trap) object representation of a signed integer type where the sign bit is zero is a valid object representation of the corresponding unsigned type, and shall represent the same value.

For any integer type, the object representation where all the bits are zero shall be a representation of the value zero in that type.



In C++11 (ISO/IEC 14882:2011)

2.3/3     Character sets     [lex.charset]

The basic execution character set and the basic execution wide-character set shall each contain all the members of the basic source character set, plus control characters representing alert, backspace, and carriage return, plus a null character (respectively, null wide character), whose representation has all zero bits.

解决方案

C++ 11

I think the pertinent part are

3.9.1/1 In C++11

For character types, all bits of the object representation participate in the value representation. For unsigned character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types.

Along with 3.9.1/7

The representations of integral types shall define values by use of a pure binary numeration system.

C11

6.2.6.2 is very explicit

For unsigned integer types other than unsigned char, the bits of the object representation shall be divided into two groups: value bits and padding bits (there need not be any of the latter). If there are N value bits, each bit shall represent a different power of 2 between 1 and 2N−1, so that objects of that type shall be capable of representing values from 0 to 2N − 1 using a pure binary representation; this shall be known as the value representation. The values of any padding bits are unspecified.

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:

— the corresponding value with sign bit 0 is negated (sign and magnitude);

— the sign bit has the value −(2M) (two’s complement);

— the sign bit has the value −(2M − 1) (ones’ complement).

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.

Summmary

I think the intend is the same for both standard.

  • char, signed char and unsigned char have all bits participating in the value

  • other integer types may have padding bits which don't participate in the value. A wrong bit pattern in them may imply a not valid value.

  • the interpretation is a pure binary representation, something whose definition is expanded in the C11 citation above.

Two things which may be not clear:

  • can -0 (for sign and magnitude and _ones' complement) be a trap value in C++

  • can one of the padding bits be a parity bit (i.e. can we modify the representation if we ensure that the padding bits aren't modified or not)

I'd be conservative and assume yes for the both.

这篇关于将int的所有字节设置为(unsigned char)0,保证表示零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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