+ 2
What does the & operator do as part of a condition?
for(int a = 0; a<10; a++){ if(a&1) { cout << a; } } // outputs 13579 What does the & operator do as part of the 'if' statement's condition?
2 ответов
+ 6
Solus it's *bitwise AND* operator that is being used inside the *if* statement.
Bitwise AND operator takes binary representation of the 2 operands and perform bitwise AND of them.
In this perticular case, you are checking wether any number's first bit is 0 or not.
It is just another(faster) way to check whether an number is even or odd.
you can learn more about bitwise operators here👇
https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/
Edit : thanks ~ swim ~ for correcting the error.