简体   繁体   English

逻辑运算符要明智吗?

[英]Logical operator to bit wise?

I'm working on a puzzle right now....trying to write if (x==5 || x==7) With bitwise operations (in C). 我正在研究一个谜题。...试图用(C)中的按位运算来编写if (x==5 || x==7) )。 Been working on it for a while....can't figure it out. 一段时间了。。。

Any help would be appreciated! 任何帮助,将不胜感激! Thanks 谢谢

Ps this isn't homework...trying to study for a test. 附言:这不是家庭作业...试图学习考试。

EDIT so the format would be something like if (x _ _) with a bitwise operation in the blanks 编辑,因此格式将类似于if (x _ _) ,空格中按位运算

SORRY need to specify, can only be two characters (operator or numeric value) So %8 for example SORRY需要指定,只能是两个字符(运算符或数字值),例如%8

7d = 111b and 5d = 101b 7d = 111b和5d = 101b

So bit 0 must be on, bit 1 is don't care, bit 2 must be on and bits 3-31 must be off. 因此位0必须为开,位1无关,位2必须为开,位3-31必须为关。 So, mask out bit 1 and test for 101b 因此,屏蔽掉位1并测试101b

so your test becomes ((x & ~2) == 5) 因此您的测试变为((x&〜2)== 5)

Then ask Bing or wikipedia about "Karnaugh Maps" so you can do your own expression reduction. 然后向Bing或Wikipedia询问有关“ Karnaugh Maps”的信息,以便您可以自己进行表情简化。

Tom's answer below is also correct and is simpler. 汤姆下面的答案也是正确的,而且更简单。 You could write 你可以写

((x & 5) == 5)

and this is slightly faster. 这会稍微快一点。 Perhaps I should have used a Karnaugh map! 也许我应该使用卡诺地图!

您可以将其与“ 101”进行“与”运算,则5和7的结果相同,即101。

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

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