I don't think there is, but is there any difference between the following expressions?
Code:... unsigned char flag ; #define BIT0 0x80 #define BIT1 0x40 ... if (flag & BIT0) ... ; if ((flag & BIT0) == BIT0) ... ; ...
I don't think there is, but is there any difference between the following expressions?
Code:... unsigned char flag ; #define BIT0 0x80 #define BIT1 0x40 ... if (flag & BIT0) ... ; if ((flag & BIT0) == BIT0) ... ; ...
Mainframe assembler programmer by trade. C coder when I can.
I appreciate your replying, but IMHO, the pertinent pieces of the question were more than enough to answer. The question was about syntax, style and expression evaluation.
However, I have tested this myself and the result is both flavors of expressions yield the same result, (regardless of how flag is set) with the latter == comparison in the second example being redundant and unnecessary.
Mainframe assembler programmer by trade. C coder when I can.
They're the same thing, so long as you're only testing one bit at a time.
If you're testing multiple bits, the former will be true if any bits match, whereas the latter will only be true if all bits match.
> with the latter == comparison in the second example being redundant and unnecessary.
I guess it depends on how you view redundant.
Sure, it's half a second of typing, a few bytes extra of disk space, and unmeasurable amounts of nanoseconds extra on the compilation.
For the human reader, the latter is rather more obvious than the former.
Even more so if the bits had names.
Code:#define SWITCH_ON_BIT 0x80 #define MOTOR_ON_BIT 0x40 ... if ((flag & SWITCH_ON_BIT) == SWITCH_ON_BIT)
Any decent compiler will generate the same code in either case (as gcc does), so there isn't a "for performance" argument to be made.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
There are a couple of differences. First, as @Salem points out, is the fact that the sameness only holds when the mask you are testing is a single bit. If your mask includes 2 or more bits, and your input only matches some of them, then the flag & MASK case will be non-zero (true) when the equality case is false.
This means if your question was really "what coding style should I prefer when writing this kind of test", you should re-think.
Second, the result of the expression is different. This doesn't matter in this specific example, because the result is being collapsed into zero/nonzero by the surrounding if (), but in other cases your flag & BIT0 will have a value of 0x80, while (flag & BIT0) == BIT0 will have a value of 1. (Or zero, of course, but that's the same in both flavors.)
Thank you both for replying. I would have responded earlier, but couldn't - see below.
I went through several test cases to see what is happening and to understand it better. I guess it's pretty straight forward once you study it.
On another note... I'm having issues with this site using Chrome. There are 2 things that are occurring often.Code:#define BIT0 0x80 #define BIT4 0x08 unsigned char flag ; 0x88 : if (flag & BIT0) is true 0x88 : if ((flag & BIT0)==0) is false 0x88 : if ((flag & BIT0)==BIT0) is true 0x88 : if (flag & (BIT0+BIT4)) is true 0x88 : if ((flag & (BIT0+BIT4))==0) is false 0x88 : if ((flag & (BIT0+BIT4))==(BIT0+BIT4)) is true 0x80 : if (flag & BIT0) is true 0x80 : if ((flag & BIT0)==0) is false 0x80 : if ((flag & BIT0)==BIT0) is true 0x80 : if (flag & (BIT0+BIT4)) is true 0x80 : if ((flag & (BIT0+BIT4))==0) is false 0x80 : if ((flag & (BIT0+BIT4))==(BIT0+BIT4)) is false 0x08 : if (flag & BIT0) is false 0x08 : if ((flag & BIT0)==0) is true 0x08 : if ((flag & BIT0)==BIT0) is false 0x08 : if (flag & (BIT0+BIT4)) is true 0x08 : if ((flag & (BIT0+BIT4))==0) is false 0x08 : if ((flag & (BIT0+BIT4))==(BIT0+BIT4)) is false 0x00 : if (flag & BIT0) is false 0x00 : if ((flag & BIT0)==0) is true 0x00 : if ((flag & BIT0)==BIT0) is false 0x00 : if (flag & (BIT0+BIT4)) is false 0x00 : if ((flag & (BIT0+BIT4))==0) is true 0x00 : if ((flag & (BIT0+BIT4))==(BIT0+BIT4)) is false
First, I get an error dialog (that I am typing in now!) that tells me my post was too short and needs to be at least 4 characters. I just tried post all the above remarks and the code inside the code tags, so I'm certainly meeting that criteria.
Second, when I come back to this site to read and check for replies, I am unable to type text in the Quick Reply box. There box appears to not be fully loaded. I'm attaching a screen shot of both issues.
Are these known issues? Is it just a Chrome thing? I'm up to date w/Chrome. Running Win 10.
Mainframe assembler programmer by trade. C coder when I can.
Regarding the Chrome issue.
Force reload the page to ignore your local cache (How to force refresh without cache in Google Chrome? - Super User)
Clean the cache and cookies.
Disable any ad/script blockers (post length and code tags are client side bits of Javascript).
Try incognito mode.
Try creating a new empty profile in Chrome.
Try another browser on the same machine.
> #define BIT0 0x80
Most people would pick bit 0 to mean the LSB.
For example, it wouldn't change it's meaning if you changed from 8 bits to 16 bits.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.