ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – 按位运算符可以有未定义的行为吗?

2019-10-01 12:06:47  阅读:162  来源: 互联网

标签:c-3 ones-complement c language-lawyer bitwise-operators


按位运算符(〜,&,|和^)对其提升的操作数的按位表示进行操作.这样的操作会导致未定义的行为吗?

例如,〜运算符在C标准中以这种方式定义:

6.5.3.3 Unary arithmetic operators

The result of the ~ operator is the bitwise complement of its (promoted) operand (that is, each bit in the result is set if and only if the corresponding bit in the converted operand is not set). The integer promotions are performed on the operand, and the result has the promoted type. If the promoted type is an unsigned type, the expression ~E is equivalent to the maximum value representable in that type minus E.

在所有体系结构中,~0产生一个位模式,其符号位设置为1,所有值位设置为1.在一个补码架构上,此表示对应于负零.这个位模式可以成为陷阱表示吗?

是否存在涉及更常见架构的简单位运算符的未定义行为的其他示例?

解决方法:

对于一个补码系统,明确列出了那些在有符号整数中不支持负零的陷阱值的可能性(C11 6.2.6.2p4):

If the implementation does not support negative zeros, the behavior of the &, |, ^, ~, <<, and >> operators with operands that would produce such a value is undefined.

然后,一个人的补充系统并不完全相同;例如GCC不支持任何!

C11确实意味着signed types(C11 6.5p4)允许实现定义和未定义的方面.

标签:c-3,ones-complement,c,language-lawyer,bitwise-operators
来源: https://codeday.me/bug/20191001/1838765.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有