5 5 votes What will be returned by the following function foo when called as foo(10)? int foo(int n) { return n & n | 1; } Programming in C go-programming-1 programming numerical-answers bitwise-operations two-marks + – Arjun 2.7k views answer comment Share Follow Print See 1 comment 1 1 comment reply anon1 commented Aug 27, 2020 reply Follow flag 10 in binary form is 1010 n=1010 we have to calculate => n&n|1 = (n & n) | 1 = (1010 & 1010) | 0001 = 1010 | 0001 = 1011 = 11(in decimal). so the ans is 11. 0 0 replyShare Please log in or register to add a comment.
Best answer 9 9 votes & : Bitwise AND operator | : Bitwise OR operator int foo(int n) { return n & n | 1; } return n & n | 1; here precedence of &>| so n&n will be executed 1st. n&n = n Now, n|1 which is nothing but n+1 for even n (least bit 0) and n for odd n. For foo(10), 11 will be output. Digvijay Pandey answered Oct 20, 2016 • edited Nov 18, 2016 by Arjun Digvijay Pandey comment Share Follow See all 3 Comments 3 3 Comments reply the.brahmin.guy commented Nov 2, 2016 reply Follow flag can you please explain binary operations in some detail ? 0 0 replyShare Rajesh Pradhan commented Nov 18, 2016 reply Follow flag @Digvijay I think we cant say here :-> foo(int n) it will return n+1 as ur statement ( n|1 which is nothing but n+1.) is seeming me wrong. Bcz suppose n=11 means foo(11) n&n=n=00001011 now n|1=00001011 | 00000001 = 00001011=n=11 itself. so foo(11) will return 11 ; not 12. plz correct me :) 8 8 replyShare Arjun commented Nov 18, 2016 reply Follow flag yes, you are correct.. 1 1 replyShare Please log in or register to add a comment.
1 1 vote foo will return 11 because 10 &10 will return 10 and logical OR will return 10I1 means 10+1=11 SHIVAM GARG answered Aug 19, 2018 SHIVAM GARG comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote 10 bitwise-AND 10 bitwise-OR 1 => 1010 AND 1010 OR 0001 in binary => 1010 OR 0001 => 1011 => 11 in decimal. JashanArora answered Dec 13, 2019 JashanArora comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes I think in OR we stop the computation after we encounter TRUE(1) for the 1st argument. Amit Tiwari 5 answered Aug 18, 2020 Amit Tiwari 5 comment Share Follow 0 reply Please log in or register to add a comment.