I’m working on a puzzle right now….trying to write
if (x==5 || x==7)
With bitwise operations (in C). 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
SORRY need to specify, can only be two characters (operator or numeric value)
So %8 for example
7d = 111b and 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.
So, mask out bit 1 and test for 101b
so your test becomes ((x & ~2) == 5)
Then ask Bing or wikipedia about “Karnaugh Maps” so you can do your own expression reduction.
Tom’s answer below is also correct and is simpler. You could write
and this is slightly faster. Perhaps I should have used a Karnaugh map!