本文介绍了运算符Precent问题/问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 专家, 我写了(并交付了:-()一些代码。我想检查一下P8的底部两位是否为设置,所以我制作了: if(P8&(uint8)0x03 ==(uint8)0x03){ //代码 } 此代码中存在一个错误,即等于优先级优先于b $ b,因此我的代码表现为: if(P8&((uint8)0x03 ==(uint8)0x03)){ //代码 } 0x03 == 0x03总是等于TRUE。因此我的代码相当于 if(P8& TRUE){ //代码 } 所以我的问题是,TRUE怎么可能被代表,什么时候会出现 代码运行。 (我完全希望答案是这取决于编译器) 谢谢 Steven(通常使用强类型VHDL) 解决方案 简化,不失一般性:如果(x& y == z) 是的,这是一个令人讨厌的,那个; dmr承认这是一个错误,它应该在早期阶段得到修复。现在太晚了。 是的。更简单: if(x&(y == z)) 好​​吧,无论是真还是假。如果它是假的,它将产生0.因为 它是真的,它产生1.(所有条件表达式都以这种方式运行。) 更确切地说,并使用我的概括: if(x& 1) 如果设置了 第一个值的低位,将执行成功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) 简化,不失一般性y:if(x& y == z) 是的,这是一个讨厌的,那个; dmr承认这是一个错误,它应该在早期阶段得到修复。现在太晚了。 是的。更简单: if(x&(y == z)) 好​​吧,无论是真还是假。如果它是假的,它将产生0.因为 它是真的,它产生1.(所有条件表达式都以这种方式运行。) 更确切地说,并使用我的概括: if(x& 1) 如果设置了 第一个值的低位,将执行成功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) 什么是uint8?标准C没有这种类型,但是有uint8_t。 在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 thatthe 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 precedencethan bitwise and. Therefore my code behaves as:if (P8 & ((uint8)0x03 == (uint8)0x03)) {// Code}0x03==0x03 always equates to TRUE. Therefore my code equates toif (P8 & TRUE) {// code}So my question is, how is TRUE likely to be represented, and when willthe code be run.(I fully expect the answer to be "It depends on the compiler")ThanksSteven (who usually uses the strongly typed VHDL) 解决方案Simplifying, without loss of generality: if(x & y == z)Yes, it''s a nasty one, that; dmr acknowledges that it was a mistake whichought to have been fixed at an early stage. Too late now, of course.Yes. More simply:if(x & (y == z))Well, it is either true or false. If it were false, it would yield 0. Sinceit is true, it yields 1. (All conditional expressions behave in this way.)More precisely, and using my generalisation:if(x & 1)The statement that succeeds the if() will be executed if the low bit of yourfirst value is set - i.e. if(x & 1) evaluates to 1.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 equalto) shall yield 1 if the specified relation is true and 0 ifit is false.80) The result has type int.6.5.9 [...] Each of the operators yields 1 if thespecified relation is true and 0 if it is false. The resulthas type int.--Richard Heathfield"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.ukemail: rjh at above domain (but drop the www, obviously)Simplifying, without loss of generality: if(x & y == z)Yes, it''s a nasty one, that; dmr acknowledges that it was a mistake whichought to have been fixed at an early stage. Too late now, of course.Yes. More simply:if(x & (y == z))Well, it is either true or false. If it were false, it would yield 0. Sinceit is true, it yields 1. (All conditional expressions behave in this way.)More precisely, and using my generalisation:if(x & 1)The statement that succeeds the if() will be executed if the low bit of yourfirst value is set - i.e. if(x & 1) evaluates to 1.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.ukemail: rjh at above domain (but drop the www, obviously)What is uint8? Standard C has no such type, but has uint8_t.In C, the relational operators always return 1 for true and 0 forfalse, so in your case P8 would be bitwise and''ed with 0x01. So the IFwill evaluate to true if the Least Significant Bit of P8 is set, inwhich case the statements inside the IF block will execute, otherwisecontrol will transfer to the statement after the IF block, (maybe anELSE?).I don''t know for sure, but I don''t believe compilers will differ overthis. The Standard guarentees that relational expressions will yieldeither 1 or 0, so conforming compilers, (compiling in a conformingmode), should all behave the same.Whether the IF evaluates true or not depends on the bit representationof P8, (i.e. it''s vaue), which only you know. 这篇关于运算符Precent问题/问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-16 00:38
查看更多