简体   繁体   English

C ++按位OR运算符

[英]C++ bitwise OR operator

bool OrderUtils::shouldCptyAutoexecute(int Id)
{

bool res = 
    dummyCache::instance().getVal(Id).getWhitelabelType() == "ABCD";
if (!res)
    res |= dummyCache::instance().getVal(Id).getIsPlc() == 1;

return res;
}

The above code checks for 2 Id's and returns true to res if any of the id is present in the database. 上面的代码检查2 Id,如果数据库中存在任何id,则返回true。

Can you suggest a way in which I can compare one more value from the databse table and return true to the value res..Also can you explain what does the second if statement do and the bitwise OR operator? 你能建议一种方法,我可以比较数据库表中的另一个值,并返回true res值。还可以解释第二个if语句和按位OR运算符是什么?

Sir, just let the short-circuit eval do this for you: 先生,让短路评估为您做到这一点:

return dummyCache::instance().getVal(Id).getWhitelabelType() == "ABCD" ||
       dummyCache::instance().getVal(Id).getIsPlc() == 1;

If the first is true, the second will not fire. 如果第一个是真的,第二个不会开火。 Moreover, I assure you a remotely-reasonable optimizing compiler will not re-fire instance().getVal(id) if the returned object has not changed between the getWhitelabelType() and getisPlc() calls. 此外,我向你保证,如果返回的对象在getWhitelabelType()getisPlc()调用之间没有改变,那么远程合理的优化编译器将不会重新触发instance().getVal(id) In fact, i would all-but-guarantee it if getWhiteLabelType() is const . 事实上,如果getWhiteLabelType()const ,我会全部保证。 (and it certainly looks like it should be). (当然看起来应该是这样)。

Regarding the bit work. 关于位工作。 The expression was pretty-much whacked. 表达非常糟糕。 though it will work. 虽然它会起作用。 Unless I read it wrong (and trust me, the list of people that will tell me I am will let me know quickly) it is performing a boolean eval, promoting the resulting true/false bool to an int , promoting the current value of res from bool to int (which is zero, so nothing special there), bitwise-OR-ing that with the expression int , then demoting the final int back to a bool to store in res . 除非我读错了(并且相信我,会告诉我的人员列表会让我快速通知)它正在执行布尔值,将结果的真/假bool提升为int ,提升res的当前值从boolint (这是零,所以没有什么特别之处),使用表达式int按位或运算,然后将最终的int降级为bool以存储在res I'm surprised this doesn't flag at least a warning on the compiler. 我很惊讶这至少没有标记编译器的警告。

It likely should have been if (!res) res ||= expr , and even then, it is pointless, as you can just use short circuit eval as in the top of this answer to cut out the local res entirely. 它可能应该是if (!res) res ||= expr ,即便如此,它也是毫无意义的,因为你可以在这个答案的顶部使用短路eval来完全切断本地res Consider if res were false . 考虑resfalse Then the equivalent expression would be res = false || expr 那么等价表达式将是res = false || expr res = false || expr . res = false || expr But thats just res = expr . 但那只是res = expr In the !res state it executes in, you may as well just use an assignment. 在执行的!res状态中,您也可以使用赋值。

Finally, regarding adding a third field to your eval, it depends entirely on how you want it added. 最后,关于向您的eval添加第三个字段,它完全取决于您希望如何添加它。 for an additional logical OR it is pretty simple. 对于一个额外的逻辑OR,它非常简单。

For an expression like (A || B || C) you can just 对于像(A || B || C)这样的表达式,你可以

return dummyCache::instance().getVal(Id).AField() == ATestValue ||
       dummyCache::instance().getVal(Id).BField() == BTestValue ||
       dummyCache::instance().getVal(Id).CField() == CTestValue;

For more complex operations, some judicious use of parenthesis will go a long way. 对于更复杂的操作,明智地使用括号将会有很长的路要走。 For example, to return (A || B) && C : 例如,要返回(A || B) && C

return (dummyCache::instance().getVal(Id).AField() == ATestValue ||
        dummyCache::instance().getVal(Id).BField() == BTestValue) &&
       dummyCache::instance().getVal(Id).CField() == CTestValue;

Or perhaps (A && C) || (B && !C) 或者(A && C) || (B && !C) (A && C) || (B && !C) (ok this is getting a little overboard...) (A && C) || (B && !C) (好的,这有点过分...)

return (dummyCache::instance().getVal(Id).CField() == CTestValue) 
       ? (dummyCache::instance().getVal(Id).AField() == ATestValue)
       : (dummyCache::instance().getVal(Id).BField() == BTestValue);

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

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