简体   繁体   English

是什么类型的操作?

[英]Is typecasting a unary operation?

In C, is typecasting a variable/value considered a unary operation? 在C中,是否将变量/值类型转换为一元运算?

And to add to this is typecasting an integer value (perhaps before you apply a mask to it) also a unary operation? 要添加到这个是类型化整数值(可能在你应用掩码之前)也是一元操作? Let's say I have 让我说我有

((uint64_t)1 << 30 & 0xFFFF0000FFFF0000LL)

Is 1 padded with zeros (up to 64 bits) and then shifted over? 1填充零(最多64位),然后移位?

Logically, yes, it's reasonable to think of it as a unary operator since it takes one operand. 逻辑上,是的,将它视为一元运算符是合理的,因为它需要一个操作数。

But if you look at the way the C standard classifies expressions, unary operators are covered in section 6.5.3, and the cast operator is described separately in section 6.5.4. 但是如果你看一下C标准对表达式进行分类的方式,第6.5.3节将介绍一元运算符,并在第6.5.4节中单独描述了强制运算符。

There actually is at least one case where the distinction matters. 实际上至少有一种区别很重要。

sizeof unary-expresson sizeof 单目expresson

is one of the several forms of unary-expression . 是一元表达的几种形式之一。 If a cast operator (type-name) were treated as a unary operator like the others, then this: 如果一个强制转换操作符(type-name)被视为像其他操作符一样的一元运算符,那么:

sizeof (int) 42

would be a valid unary expression: 42 would be the operand of the cast operator, which in turn would be the operand of the sizeof unary operator. 将是一个有效的一元表达式: 42将是转换运算符的操作数,而操作数又是一元运算符sizeof的操作数。 But in fact it's a syntax error (because sizeof (int) by itself is a valid expression). 但实际上这是一个语法错误(因为sizeof (int)本身就是一个有效的表达式)。 To avoid that problem, unary operators and the cast operator are defined separately (in effect given different precedences). 为了避免这个问题,一元运算符和强制转换运算符是分开定义的(实际上给出了不同的优先级)。 To write the above while avoiding a syntax error, you need to add parentheses: 要在避免语法错误的同时编写上述内容,您需要添加括号:

sizeof ((int) 42)

Reference: N1570 , the latest freely available draft of the 2011 ISO C standard. 参考: N1570,2011 ISO C标准的最新免费草案。

Being a unary operator means nothing more than having exactly one argument. 作为一元运算符只不过只有一个参数。 The type parameter doesn't cound as an argument, so the casting operator is unary. type参数不作为参数,因此转换运算符是一元的。

Your second question is related to operator precedence. 您的第二个问题与运营商优先级有关。 A cast has the same precedence as other unary operators, which is higher than shifting << which in turn is higher than binary and & . 强制转换具有与其他一元运算符相同的优先级,高于移位<< ,而高于二进制和&

So your code is equivalent to: 所以你的代码相当于:

((((uint64_t)1) << 30) & 0xFFFF0000FFFF0000LL)

The C standards do not talk specifically about "unary operations". C标准没有具体谈到“一元操作”。 They have "unary operators" ("-ors" vs "-ions"), and casts are not part of the former as they are listed separately. 他们有“一元运算符”(“-ors”vs“-ions”),而演员不属于前者,因为它们是单独列出的。

Mathematically, however, they are definitely unary: they have one input and one output. 然而,在数学上,它们绝对是一元的:它们有一个输入和一个输出。

Your question appears not to be about the terminology , though, but rather about the binding : does 你的问题似乎不是关于术语 ,而是关于绑定 :是的

(uint64_t)1 << 30

bind as: 绑定为:

((uint64_t)1) << 30

for instance, and then does all that bind together so that the & operation is applied to its result. 例如,然后执行所有绑定在一起的操作,以便将&操作应用于其结果。 The answer here is yes, they do. 答案是肯定的,他们这样做。 While the standards don't use an operator precedence grammar, you can derive one from the standard grammars, and in such grammars, casts and unary operators are higher precedence than shift operators, which are higher precedence than bitwise operators. 虽然标准不使用运算符优先级语法,但您可以从标准语法中派生出一个,而在这样的语法中,强制转换运算符和一元运算符的优先级高于移位运算符,后者的优先级高于按位运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM