运算符Precent问题/问题 [英] Operator Precence Issue/Question

查看:60
本文介绍了运算符Precent问题/问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专家,


我写了(并交付了:-()一些代码。我想检查一下P8的底部两位是否为
设置,所以我制作了:


if(P8&(uint8)0x03 ==(uint8)0x03){

//代码

}


此代码中存在一个错误,即等于优先级优先于b
$ b,因此我的代码表现为:


if(P8&((uint8)0x03 ==(uint8)0x03)){

//代码

}


0x03 == 0x03总是等于TRUE。因此我的代码相当于


if(P8& TRUE){

//代码

}


所以我的问题是,TRUE怎么可能被代表,什么时候会出现

代码运行。


(我完全希望答案是这取决于编译器)


谢谢


Steven(通常使用强类型VHDL)

解决方案

mo****@yahoo.co.uk 说:


专家,


我写过(并交付:-()一些代码。我想检查一下P8的底部两位是否设置了

,所以我制作了:


if(P8&(uint8)0x03 == (uint8)0x03){

//代码

}



简化,不失一般性:如果(x& y == z)


是的,这是一个令人讨厌的,那个; dmr承认这是一个错误,它应该在早期阶段得到修复。现在太晚了。


>

此代码中有一个错误,即等于优先级更高

比按位和。因此我的代码表现为:


if(P8&((uint8)0x03 ==(uint8)0x03)){



是的。更简单:


if(x&(y == z))


//代码

}


0x03 == 0x03总是等于TRUE。



好​​吧,无论是真还是假。如果它是假的,它将产生0.因为

它是真的,它产生1.(所有条件表达式都以这种方式运行。)


因此我的代码相当于


if(P8& TRUE){

//代码



更确切地说,并使用我的概括:


if(x& 1)


}


所以我的问题是,TRUE可能如何表示,以及何时运行代码




如果设置了

第一个值的低位,将执行成功if()的语句 - 即if(x) & 1)评估为1.


(我完全希望答案是它取决于编译器)



为了怜悯,它实际上并不依赖于编译器!

运算符==!=< < = =总是产生0或1作为结果,

并且由标准保证:

6.5.8 [...]每个运营商< (小于),(大于),

< =(小于或等于),> =(大于或等于

to)将产生如果指定的关系为真,则为1;如果为
则为0,则为0.80)结果的类型为int。


6.5.9 [...]如果

指定的关系为真,则每个运算符产生1,如果为假,则为0。结果

的类型为int。


-

Richard Heathfield

" Usenet is一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


Richard,

感谢您的全面而详细的回复。


幸运的是,它也是我想要的答案(我通过它来回答

意味着我可以在没有人知道的情况下工作:-))

史蒂文


Richard Heathfield写道:
< blockquote class =post_quotes> mo****@yahoo.co.uk 说:


专家,


我写过(并交付:-()一些代码。我想检查一下

P8的底部两位已设置,因此我制作了:


if(P8&(uint8)0x03 ==(uint8)0x03){

//代码

}



简化,不失一般性y:if(x& y == z)


是的,这是一个讨厌的,那个; dmr承认这是一个错误,它应该在早期阶段得到修复。现在太晚了。



这段代码中有一个错误,即等于优先级更高

比按位和。因此我的代码表现为:


if(P8&((uint8)0x03 ==(uint8)0x03)){



是的。更简单:


if(x&(y == z))


//代码

}


0x03 == 0x03总是等于TRUE。



好​​吧,无论是真还是假。如果它是假的,它将产生0.因为

它是真的,它产生1.(所有条件表达式都以这种方式运行。)


因此我的代码相当于


if(P8& TRUE){

//代码



更确切地说,并使用我的概括:


if(x& 1)


}


所以我的问题是,TRUE可能如何表示,以及何时运行代码




如果设置了

第一个值的低位,将执行成功if()的语句 - 即if(x) & 1)评估为1.


(我完全希望答案是它取决于编译器)



为了怜悯,它实际上并不依赖于编译器!

运算符==!=< < = =总是产生0或1作为结果,

并且由标准保证:


6.5.8 [...]每个运营商< (小于),(大于),

< =(小于或等于),> =(大于或等于

to)将产生如果指定的关系为真,则为1;如果为
则为0,则为0.80)结果的类型为int。


6.5.9 [...]如果

指定的关系为真,则每个运算符产生1,如果为假,则为0。结果

的类型为int。


-

Richard Heathfield

" Usenet is一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


mo **** @ yahoo.co.uk 写道:


专家,


我写了(并交付了:-()一些代码。我想检查一下P8的底部两位是否设置了

,所以我制作:


if(P8&(uint8)0x03 ==(uint8)0x03){

//代码

}



什么是uint8?标准C没有这种类型,但是有uint8_t。


有这个代码中的错误是优先级比优先级更高优先级

因此我的代码表现为:


if(P8&((uint8) 0x03 ==(uint8)0x03)){

//代码

}


0x03 == 0x03总是s等于TRUE。因此我的代码相当于


if(P8& TRUE){

//代码

}


所以我的问题是,如何表示TRUE,以及何时运行代码




在C中,关系运算符总是返回1表示true,0表示

false,因此在您的情况下P8将是按位且'用0x01编辑。因此如果设置了P8的最低有效位,IF

将评估为真,在
中,IF块内的语句将执行,否则

控制将转移到IF块之后的语句,(可能是一个

ELSE?)。


(我完全期待答案是它取决于编译器)



我不确定,但我不相信编译器会有所不同

这个。标准保证关系表达式将产生1或0的
,因此符合标准的编译器(以符合

模式编译)应该都表现相同。


IF评估是否为真取决于P8的位代表

,(即它是vaue),只有你知道。


Experts,

I have written (and delivered :-( ) some code. I wanted to check that
the bottom two bits of P8 were set, and so I produced:

if (P8 & (uint8)0x03 == (uint8)0x03) {
// Code
}

There is a mistake in this code in that equality is higher precedence
than bitwise and. Therefore my code behaves as:

if (P8 & ((uint8)0x03 == (uint8)0x03)) {
// Code
}

0x03==0x03 always equates to TRUE. Therefore my code equates to

if (P8 & TRUE) {
// code
}

So my question is, how is TRUE likely to be represented, and when will
the code be run.

(I fully expect the answer to be "It depends on the compiler")

Thanks

Steven (who usually uses the strongly typed VHDL)

解决方案

mo****@yahoo.co.uk said:

Experts,

I have written (and delivered :-( ) some code. I wanted to check that
the bottom two bits of P8 were set, and so I produced:

if (P8 & (uint8)0x03 == (uint8)0x03) {
// Code
}

Simplifying, without loss of generality: if(x & y == z)

Yes, it''s a nasty one, that; dmr acknowledges that it was a mistake which
ought to have been fixed at an early stage. Too late now, of course.

>
There is a mistake in this code in that equality is higher precedence
than bitwise and. Therefore my code behaves as:

if (P8 & ((uint8)0x03 == (uint8)0x03)) {

Yes. More simply:

if(x & (y == z))

// Code
}

0x03==0x03 always equates to TRUE.

Well, it is either true or false. If it were false, it would yield 0. Since
it is true, it yields 1. (All conditional expressions behave in this way.)

Therefore my code equates to

if (P8 & TRUE) {
// code

More precisely, and using my generalisation:

if(x & 1)

}

So my question is, how is TRUE likely to be represented, and when will
the code be run.

The statement that succeeds the if() will be executed if the low bit of your
first value is set - i.e. if(x & 1) evaluates to 1.

(I fully expect the answer to be "It depends on the compiler")

For a mercy, it actually doesn''t depend on the compiler on this occasion!
The operators == != < <= = always yield either 0 or 1 as their result,
and that is guaranteed by the Standard:
6.5.8 [...] Each of the operators < (less than), (greater than),
<= (less than or equal to), and >= (greater than or equal
to) shall yield 1 if the specified relation is true and 0 if
it is false.80) The result has type int.

6.5.9 [...] Each of the operators yields 1 if the
specified relation is true and 0 if it is false. The result
has type int.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Richard,

Thank you for the full and detailed response.

And as luck would have it, it''s also the answer I wanted (by which I
mean I can work around without anyone knowing :-) )

Steven

Richard Heathfield wrote:

mo****@yahoo.co.uk said:

Experts,

I have written (and delivered :-( ) some code. I wanted to check that
the bottom two bits of P8 were set, and so I produced:

if (P8 & (uint8)0x03 == (uint8)0x03) {
// Code
}


Simplifying, without loss of generality: if(x & y == z)

Yes, it''s a nasty one, that; dmr acknowledges that it was a mistake which
ought to have been fixed at an early stage. Too late now, of course.


There is a mistake in this code in that equality is higher precedence
than bitwise and. Therefore my code behaves as:

if (P8 & ((uint8)0x03 == (uint8)0x03)) {


Yes. More simply:

if(x & (y == z))

// Code
}

0x03==0x03 always equates to TRUE.


Well, it is either true or false. If it were false, it would yield 0. Since
it is true, it yields 1. (All conditional expressions behave in this way.)

Therefore my code equates to

if (P8 & TRUE) {
// code


More precisely, and using my generalisation:

if(x & 1)

}

So my question is, how is TRUE likely to be represented, and when will
the code be run.


The statement that succeeds the if() will be executed if the low bit of your
first value is set - i.e. if(x & 1) evaluates to 1.

(I fully expect the answer to be "It depends on the compiler")


For a mercy, it actually doesn''t depend on the compiler on this occasion!
The operators == != < <= = always yield either 0 or 1 as their result,
and that is guaranteed by the Standard:
6.5.8 [...] Each of the operators < (less than), (greater than),
<= (less than or equal to), and >= (greater than or equal
to) shall yield 1 if the specified relation is true and 0 if
it is false.80) The result has type int.

6.5.9 [...] Each of the operators yields 1 if the
specified relation is true and 0 if it is false. The result
has type int.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


mo****@yahoo.co.uk wrote:

Experts,

I have written (and delivered :-( ) some code. I wanted to check that
the bottom two bits of P8 were set, and so I produced:

if (P8 & (uint8)0x03 == (uint8)0x03) {
// Code
}

What is uint8? Standard C has no such type, but has uint8_t.

There is a mistake in this code in that equality is higher precedence
than bitwise and. Therefore my code behaves as:

if (P8 & ((uint8)0x03 == (uint8)0x03)) {
// Code
}

0x03==0x03 always equates to TRUE. Therefore my code equates to

if (P8 & TRUE) {
// code
}

So my question is, how is TRUE likely to be represented, and when will
the code be run.

In C, the relational operators always return 1 for true and 0 for
false, so in your case P8 would be bitwise and''ed with 0x01. So the IF
will evaluate to true if the Least Significant Bit of P8 is set, in
which case the statements inside the IF block will execute, otherwise
control will transfer to the statement after the IF block, (maybe an
ELSE?).

(I fully expect the answer to be "It depends on the compiler")

I don''t know for sure, but I don''t believe compilers will differ over
this. The Standard guarentees that relational expressions will yield
either 1 or 0, so conforming compilers, (compiling in a conforming
mode), should all behave the same.

Whether the IF evaluates true or not depends on the bit representation
of P8, (i.e. it''s vaue), which only you know.


这篇关于运算符Precent问题/问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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