In C language, I often see if statements in such form:
#define STATUS 0x000A
UINT16 InterruptStatus;
if (InterruptStatus & STATUS)
{
<do something here....>
}
If I had this statement, would it be any different in processing time or any other reason why this would not be the preferred/alternative way?
#define STATUS 0x000A
UINT16 InterruptStatus;
if (InterruptStatus == STATUS)
{
<do something here....>
}
Well, they are not the same.
In case of a bitwise AND, two different values of operands can produce a true, whereas for equality, both must be same.
Consider decimal value 5 and 3 as operands.
5 & 3 == 1).5 ==3 ==> false)So they are not alternatives, really.
Bitwise operations are widely used to check a particular bit of a flag variable to be "set" or "unset".
This can be used for checking if some number has some bits set or not, so it is different from equality.
Assume you have some number represented in binary as 0b00010001
And you want to check if bit number 4 is set, so you need to do
if(0b00010001 & 0b00010000)
// do something.
So not necessarily the two numbers above are equal - however, using above check you can verify if 4th bit is set on the 0b00010001 number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With