10th Oct 2017, 2:45 PM
zoufisha khan
5 ответов
+ 14
for(int i = 0; i < 10; i++) { if(i&1) cout << i; } Brute force output: "Cycle #1" 0000 = i = 0 0001 & ______ 0000 = 0 no output. /////// "Cycle #2" 0001 = i = 1 0001 & ______ 0001 = 1 output: 1 ////// "Cycle #3" 0010 = i = 2 0001 & ______ 0000 = 0 output still 1 /////// "Cycle #4" 0011 = i = 3 0001 & ______ 0001 = 1 new output: 11 ////// "Cycle #5" 0100 = i = 4 0001 & ______ 0000 = 0 output still 11 /////// "Cycle #6" 0101 = i = 5 0001 & ______ 0001 = 1 new output: 111 /////// "Cycle #7" 0110 = i = 6 0001 & ______ 0000 = 0 output still 111 //////// "Cycle #8" 0111 = i = 7 0001 & ______ 0001 = 1 new output: 1111 /////// "Cycle #9" 1000 = i = 8 0001 & ______ 0000 = 0 output still 1111 /////// "Cycle #10" 1001 = i = 9 0001 & ______ 0001 = 1 new output: 11111 As you see, when i is an odd number (1, 3, 5, ...) you have output.
10th Oct 2017, 3:31 PM
Babak
Babak - avatar
+ 2
& here is bitwise AND. A bitwise AND takes two binary representation and performs the logical ANDoperation on each pair of the corresponding bits, by multiplying them. Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1 (1 × 1 = 1); otherwise, the result is 0 (1 × 0 = 0 and 0 × 0 = 0).
10th Oct 2017, 2:56 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 1
Let i be 5 or any odd number then 5&1 = 101&1 [ 5 in binary is 101 ] or 101 ×1 ——— 101 Last bit is 1 so it returns true Let i be 6 or any even number then 6&1 = 110&1 [ 6 in binary is 110 ] or 110 ×1 ——— 110 Last bit is 0 so it returns false
10th Oct 2017, 2:50 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 1
what's the meaning of & operator
10th Oct 2017, 2:52 PM
zoufisha khan
0
Слишком глубоко для ответа на кратковременый вопрос: ,,Какой выход?''
8th Jun 2019, 4:37 AM
Валерий