-2
#include<iostream>
using namespace std;
int main()
    {

    int n;
    cin>>n;
    if(n&1==0)   //without using bracket  (n&1)
        cout<<"Number is even";
    else
        cout<<"Number is odd";
    return 0;
    }

output : odd //for n=6

#include<iostream>
    using namespace std;
    int main()
        {
        int n;
        cin>>n;
        if((n&1)==0)
            cout<<"Number is even";
        else
            cout<<"Number is odd";
        return 0;
        }

output : even //for n=6

Do we have to use parentheses whenever we use bitwise operators?

Nishant sharma
  • 116
  • 1
  • 11
  • 15
    [operator precedence](http://en.cppreference.com/w/cpp/language/operator_precedence), the equality operator has higher precedence than the bitwise operators hence the need for parentheses – EdChum Mar 15 '17 at 16:24
  • Yeah my mistake. – Nishant sharma Mar 15 '17 at 16:32
  • @EdChum I would use a stronger "need for parentheses" viz. any mixing of operators. Just easier that way – Caleth Mar 15 '17 at 16:33
  • 1
    And that's why you should have your warnings on. My GCC says `warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]` about the first snippet. – HolyBlackCat Mar 15 '17 at 16:37
  • @Caleth yes that's true, it becomes even more important once you start using multiple conditions – EdChum Mar 15 '17 at 16:44
  • 4
    Including parentheses whenever there is any question about order-of-operations issues will help readers of your code understand the behavior your intended to implement -- without them, they will have to wonder if they are looking at a subtle order-of-operations bug, or not. – Jeremy Friesner Mar 15 '17 at 16:44

4 Answers4

3

According to operator precedence this expression:

n&1==0

is equivalent to:

n&(1==0)

which means result of operation 1==0 which is always false is used with binary AND with n. Boolean false is implicitly converted to int which is required by binary operation and value is 0. As binary AND with 0 is always 0 that code is convoluted way to say:

if(0) 
    cout<<"Number is even";
else
    cout<<"Number is odd";

and 0 converted back to boolean as false so you always get "Number is odd" output. So yes, brackets are required in this case.

my question is do we have to put bracket whenever we are using bitwise operators?

No, you have to use them when other operations with higher precedence are involved. It does not have to be always, but if you prefer you can always put them.

Slava
  • 43,454
  • 1
  • 47
  • 90
2

my question is do we have to put bracket whenever we are using bitwise operators? or else we got logical error in our program.

If you look at the C++ operator precedence table, you'll notice that the equality operator (==) has higher precedence than the bitwise operators.

Hence, n&1==0 is interpreted as n & ( 1 == 0 ). To make sure that n & 1 is given higher precedence than 1 == 0, you will need to use parenthesis: (n & 1) == 0. (As an aside, you could have used n % 2 == 0 with expected results but I would still recommend using (n % 2) == 0).

That answers the specific question of whether parenthesis need to be used when dealing with bitwise operators and the equality operator.

To answer the general question of whether parenthesis need to be used when dealing with bitwise operators and all other operators, you'll need to use the operator precedence table as the guide.

As a general practice, I recommend using parenthesis to make the code more readable and the intention more clear. It also works when you are not always sure about the order of precedence of the operators involved.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

You actually don't need to compare to 0 here. MSVC will perhaps deliver a performance warning, but you can just say if (i%2) { odd; } else { even; }.

There's of course debate about readability and such, but any C++ dev should know what that does.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • 1
    The expression `if (i&1)` *may* be faster, depending on the optimization settings for the compiler and the compilers optimization technology. If the compiler recognizes `i%2`, it may convert it to `i&1`. – Thomas Matthews Mar 15 '17 at 17:00
0

As EdChum mentioned in his comment: the equality operator has a higher precedence than the bitwise and, i. e. the equality will be evaluated first, just as the multiplication is evaluated first because of higher precedence in this term:

if(1 + 3 * 0 == 0) // won't be true!

(I personally was never happy about this, always would have preferred the bitwise operators having higher precedence - but the standard is as it is, we have to live with...)

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • Honestly, this is another reason I prefer C#'s behavior around the `bool` type. Equality and relational operators evaluate as `bool`, and `bool` can't be used with arithmetic or bitwise operators, so even if operator precedence binds the equality operator first, you get a compile time error when you try to do `int & bool`. – cdhowie Mar 15 '17 at 16:50