KEEP K.I.S.S.

tk's blog

C 语言中的位取反操作符 续

tk posted @ Apr 27, 2012 10:54:16 AM in C with tags 位操作符 , 2220 阅读

前一篇文章的问题,在我查阅了《The C Programming Language》(K&R) 第二版后,发现了如下的定义

在附录A 参考手册的 A.7.4 一元运算符 二进制反码运算符 中有如下表述

一元运算符 ~ 的操作数必须是整型,结果为操作数的二进制反码。在运算过程中需要对操作数进行整形提升。如果操作数为无符号类型,则结果为提升后的类型能够表示的最大值减去操作数的值而得到的结果值。如果操作数为带符号型,则结果的计算方式为:将提升后的操作数转换为相应的无符号类型,使用运算符 ~ 计算反码,再将结果转换为带符号的类型结果的类型为提升后的操作数类型

对于整形提升,在附录A 参考手册的 A6.1 整形提升 有如下表述

在一个表达式中,凡是可以使用整形的地方都可以使用带符号或无符号的字符、短整型或整型位字段,还可以使用枚举类型的对象。如果原始类型的所有值都可用 int 类型表示,则其值将被转换为 int 类型;否则将被转换为 unsigned int 类型。这一过程称为整形提升(integral promotion)。

请注意红色标注的句子,“结果的类型为提升后的操作数类型”,意味着 ~(char) 、 ~(short) 等的结果类型都是整型(int)。

  • 无匹配
NCERT Computer Quest 说:
Sep 21, 2022 08:33:05 PM

One of the rational and scientific ways to progress is computer science. A rapidly expanding discipline, computer science is now in great demand. For all regional kids enrolled in Central Board schools across the nation, NCERT Computer Question Paper Class 7 the NCERT Computer Sample Paper 2023 Class 7 is provided with all Topic essential questions and suggested answers for download.

net worth 说:
Mar 06, 2024 04:40:28 PM

Checking out the information of any celebrity on idol net worth to know how much their name and fame worth and their family members.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter