3

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....>
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
newb7777
  • 517
  • 5
  • 22

5 Answers5

5

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.

  • Bitwise AND will produce a TRUE value for condition check ( 5 & 3 == 1).
  • Equality will produce a FALSE value (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".

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    Off-topic but bitwise XOR may relatively often be encountered as an (inverted) comparison. – doynax Feb 27 '17 at 07:11
2

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.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • It is used in an interesting context when an interrupt is being read from an FPGA register. From what I understand the authors intent may have been to trigger when any interrupt fires at bit position 1 or 3 ( 0x000A ), albeit counter intuitive. – newb7777 Feb 27 '17 at 07:27
1

& is a bitwise AND operator. It is used to check if a bit at a certain position is on or not.

== is an equality operator which returns true only if the values of both operands match exactly.

For more on operators in C: https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

zahirdhada
  • 405
  • 4
  • 14
0

Top one will be logically true if any one of the bit matches, 0x000A. it doesn't have to exactly equal 0x000A; it could be true if

if ((InterruptStatus == 0x0008) || (InterruptStatus == 0x0002))

The bottom code makes sure it is absolutely equal to 0x000A.

newb7777
  • 517
  • 5
  • 22
  • "it could be true if..." not only that, it also ignores the status of the other bits. IOW, it's true even if e.g. `InterruptStatus==0x28`. – Matteo Italia Feb 27 '17 at 07:26
0

Both are different.

== is a relational operator

& is a bit wise operator

From tutorials point:

== : Checks if the values of two operands are equal or not. If yes, then the condition becomes true.

& : Binary AND Operator copies a bit to the result if it exists in both operands.

For example:

A= 12 , B =13

A == B: produces output as false as these two are different

A & B: Output will be true

A = 0000 1100

B = 0000 1101

A&B = 0000 1100 -> which is 12 and it is a valid integer.so condition will be true

sreepurna
  • 1,562
  • 2
  • 17
  • 32