/*Hi,
I was trying to optimize my application in C. 
In that process to reduce the complexity at condition checks which of the below is efficient,*/


    if(bFlag1 && !(Var == 1) && bFlag2)    //or
          if(bFlag1 & !(Var == 1) & bFlag2)

&& is bitwise AND operator.
& is refrencing operator.

& cannot be used instead of &&. Aren't you getting error on using & ?

&& is bitwise AND operator.

&& is the logical AND operator.

& is refrencing operator.

& is an overloaded operator in C, but in this case it's the bitwise AND operator.

& cannot be used instead of &&.

It can in some places, but you're limited by the type restriction of bitwise operators and the lack of short circuiting. It's strongly recommended that one not mix the two, lest one be surprised by the resulting behavior.

Performance doesn't enter the picture, especially when micromanaging performance at such a low level is the wrong place 9 times out of 10.

To optimize a program:

1) remember your first priority is always an accurate program. A wrong answer might be infinitely fast, but it's still wrong, and probably useless. Get and keep your program accurate.

2) the largest optimizing you can do, (by far), is with the choice of the algorithm for the program. It's not unusual to get a 2 to 20 x faster run time, simply by using a better algorithm. Choose this with care!

3) run the program, with a profiler, and find the bottlenecks. Concentrate your optimizing work on the code where it's needed most.

4) create a small but relevant test case with typical data. You want a test case that runs for one to two minutes. Include run time from the computer, right into your test case. Now you can easily try different optimiziations, easily, and have real data - not the hand waving or hot air theorizing that we see all too often.

If you post your test case on programming boards, you are very likely to get a large response from the members - because they can d/l it and test it themselves, they get more involved.

As a general rule of thumb, if your program can be "optimized" by changing one little operator like that, then the compiler will likely do it for you.

That being said, the short-circuiting behavior mentioned by deceptikon is important to note. Using the && operator ensures that unnecessary checks are skipped if the left operand is false (because this automatically makes the entire expression false). This could potentially mean that bitwise & would be slower in some cases.

Edited 3 Years Ago by Tumlee: Adding information

The bitwise & operator looks at the left-hand-side (lhs) and the right-hand-side (rhs) of the expression and returns a word that has the common bits set (all the bits in the lhs that are 1's that correspond to the same bits that are 1's in the rhs). So, in your example, bFlag1 will respond to true if any bits are set, but in the second version, the rhs !(var == 1) will only set the first bit to a 1 if true, so the & operator will only return a set value if the first bit in bFlag1 is set.

IE, these expressions CANNOT be equivalent except under VERY specific circumstances. This is a great example of "doing what you mean, not what you say"... :-)

P.S. My advice is to take a course in formal (boolean) logic.

Edited 3 Years Ago by rubberman

Thanku for all your replays.

Edited 3 Years Ago by Falcon143: spelling misatake

This question has already been answered. Start a new discussion instead.