括号可以用C更改位运算的操作数的结果类型? [英] Can parentheses in C change the result type of operands of a bitwise operation?

查看:165
本文介绍了括号可以用C更改位运算的操作数的结果类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过一个静态分析工具喂以下code:

I have fed the following code through a static analysis tool:

u1 = (u1 ^ u2); // OK

u1 = (u1 ^ u2) & u3;  // NOT OK

u1 = (u1 ^ u2) & 10; // NOT OK

u1 = (u1 ^ u2) & 10U; // NOT OK

u1 = (unsigned char)(u1 ^ u2) & 10U; // OK

u1 = (unsigned char)(u1 ^ u2) & u3;  // OK

OK是指静态分析工具,没有抱怨。
不OK指的是静态分析工具,并抱怨 - 声称按位操作的某些操作数不是一个无符号整数

"OK" means the static analysis tool did not complain. "NOT OK" means the static analysis tool did complain -- claiming that some operand of a bitwise operation is not an unsigned integer.

这最后2行中的结果表明,该括号是造成任

The results from the last 2 lines show that the parentheses are causing either

一个。要签署一个实际的类型转换

a. an actual type conversion to signed

乙。一些静态分析工具,认为是一种类型转换为签订

b. something that the static analysis tool thinks is a type conversion to signed

我会询问静态分析工具的开发(B)。

I will ask the static analysis tool developer about (b).

但我之前,我想知道,如果可能的C语言是众所周知的做(一)?

But before I do, I would like to know if perhaps the C language is known to do (a)?

推荐答案

用C没有下文 INT 完成:如增加了两个无符号的字符时,甚至加入前,操作数转换为 INT 根据默认的促销活动。

Nothing in C is done below int: eg when adding two unsigned chars, even before the addition, the operands are converted to int according to the default promotions.

unsigned char u1, u2, u3;
u1 = 0;
u2 = 42;
u3 = u1 + u2;

在最后一行,第一个 U1 U2 转换为 INT ,那么 + 运算符应用于获得 INT 值,然后该值被转换回到 unsigned char型(当然编译器可以使用快捷键!)

In the last line, first u1 and u2 are converted to int, then the + operator is applied to obtain a int value and then that value is converted back to unsigned char (of course the compiler can use shortcuts!)

这篇关于括号可以用C更改位运算的操作数的结果类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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