11 11 votes The for loop for (i=0; i<10; ++i) printf("%d", i&1); prints 0101010101 0111111111 0000000000 1111111111 Programming in C isro2015 programming-in-c loop + – go_editor 10.1k views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply pC commented Jul 2, 2016 reply Follow flag what the answer would be if it were printf("%d", i && 1); Could someone tell why it is happpening so ? 0 0 replyShare Arjun commented Jul 2, 2016 reply Follow flag Every operator in C/C++ has a corresponding meaning and result comes from that. "&&" is a logical and operator and all logical operators return either 0 or 1. 2 2 replyShare Please log in or register to add a comment.
Best answer 21 21 votes Option A is Ans. Bcz It is Bitwise And Operation. i & 1 0000 & 0001=0 0001 & 0001=1 0010 & 0001=0 0011 & 0001=1 Here the result is 1 if LSB of i is 1(Odd no.) & it occur at alternative position So Ans is 0101010101 http://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm Rajesh Pradhan answered Jun 24, 2016 • edited Dec 17, 2016 by Rajesh Pradhan Rajesh Pradhan comment Share Follow 0 reply Please log in or register to add a comment.
7 7 votes (odd no) & 1 = 1 (even no) &1 = 0 Ans- A. 0101010101 vijaycs answered Jun 21, 2016 vijaycs comment Share Follow 0 reply Please log in or register to add a comment.
3 3 votes Answer is A.the loop runs from 0 to 9 and i&1 MEANS BITWISE AND WITH THE BINARY FORMS OF 0 TO 9 WITH 1.SO THE NUMBERS HAVING LAST BIT AS 0 GIVES OUTPUT 0 AND NUMBERS HAVING LAST BIT AS 1 GIVES OUTPUT 1. Rohan Ghosh answered Oct 12, 2015 Rohan Ghosh comment Share Follow 0 reply Please log in or register to add a comment.
3 3 votes Here key point is that for bitwise op to be 1 we must have AND operation of LSB to be 1 here since we have bitwise AND operation performed with 1 whose LSB is 1 therefore all the odd numbers shall have their LSB 's to be 1 so directly we can say that the result of bitwise AND would be 0 for all even numbers and 1 for all odd numbers.so op is option A . radha gogia answered Oct 14, 2015 radha gogia comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote i&1 is basically checking whether i is even or odd.If it's even it'll return 1 else 0. Based on this I think output should be A. arpanmukherjee answered Jul 3, 2016 arpanmukherjee comment Share Follow 0 reply Please log in or register to add a comment.