+ 5

Cout with &

Refer below code: #include <iostream> using namespace std; int main() { for(int i=0;i<10;++i) { if(i&1) { cout << i << endl; } } return 0; } Why output of same is 1 3 5 7 9 ? could not get statement of if....

12th Jun 2018, 5:33 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
8 Answers
+ 6
As Ipang stated, the if statement condition is a simple form of bit hacking to extract odd numbers and it's equivalent to if (i % 2 != 0). The for loop cycles 10 times (0-9). Let's convert each number to its binary form, since I wanna explain it in more detail of course and more than that it's a BITwise operation! 4 bits is enough for representing the whole range. 0 = 0000 1 = 0001 2 = 0010 3 = 0011 4 = 0100 5 = 0101 6 = 0110 7 = 0111 8 = 1000 9 = 1001 Now let's say, we wanna evaluate number 7. What we are gonna do is, nothing but ANDing binary form of number 7 with 1 (I suppose you know how to do that). 0111 0001 & ________ 0001 Yay! you got 1 which means true which means it's an odd number. what if it is an even one? Let's say 4. 0100 0001 & ________ 0000 Whoops! you got 0 which means it's an even number.
12th Jun 2018, 5:58 AM
Babak
Babak - avatar
+ 14
It filters out the even numbers so you get the odd ones only.
12th Jun 2018, 5:37 AM
Ipang
+ 7
C++ Soldier (Babak) just gave an excellent explanation in detail, I was only giving a short footage of what it does. Big Thanks : )
12th Jun 2018, 6:02 AM
Ipang
+ 7
C++ Soldier (Babak) No mate! not at all, I never thought that way, as a matter of fact, was actually grateful you came up with that, to explain how it works under the hood, how and why it worked out the way it did, I value people giving explanatory answers, no issues whatsoever mate : ) Cheers Babak ; )
12th Jun 2018, 6:29 AM
Ipang
+ 5
Ketan Lalcheta Compilers use complex grammars to identify what the heck is going on in a program in different contexts. For example, & operator in [ int *y = &x; ] is considered the address (reference) operator but in [ bool b = x & y; ], bitwise AND. You see, it has some sort of analyzer and predefined patterns by which it can confidently recognizes that the operator & has 2 operand or one (Of course, our case here is easy to recognize).
12th Jun 2018, 12:08 PM
Babak
Babak - avatar
+ 4
Oh come on Ipang ! I didn't do that to render your answer less worthy. You just gave me the motivation to make it a bit lengthy! ; - ]
12th Jun 2018, 6:14 AM
Babak
Babak - avatar
+ 4
Ipang Kind and reasonable as always. SL would've been get dull without people like you, dear. Now let me mess with this post a bit! I guess OP don't mind after all! 🍪🌹🌹🌹🍪 🍪🍪🌹🍪🍪 🍪🍪🌹🍪🍪 🍪🍪🌹🍪🍪 🍪🌹🌹🌹🍪 🍪🍪🍪🍪🍪 🌹🌹🌹🌹🍪 🌹🍪🍪🍪🌹 🌹🌹🌹🌹🍪 🌹🍪🍪🍪🍪 🌹🍪🍪🍪🍪 🍪🍪🍪🍪🍪 🍪🌹🌹🌹🍪 🌹🍪🍪🍪🌹 🌹🌹🌹🌹🌹 🌹🍪🍪🍪🌹 🌹🍪🍪🍪🌹 🍪🍪🍪🍪🍪 🌹🍪🍪🍪🌹 🌹🌹🍪🍪🌹 🌹🍪🌹🍪🌹 🌹🍪🍪🌹🌹 🌹🍪🍪🍪🌹 🍪🍪🍪🍪🍪 🍪🌹🌹🌹🍪 🌹🍪🍪🍪🍪 🌹🍪🍪🌹🌹 🌹🍪🍪🍪🌹 🍪🌹🌹🌹🍪
12th Jun 2018, 6:45 AM
Babak
Babak - avatar
+ 2
thanks a lot for detailed explanation Ipang....any idea why compiler considered it as bitwise operation? does it due to the & operator added into if condition??
12th Jun 2018, 10:54 AM
Ketan Lalcheta
Ketan Lalcheta - avatar