retagged by
1,322 views
5 votes
5 votes

What will be returned by the following function foo when called as foo(10)?

int foo(int n)
{
    return n & n | 1;
}
retagged by

4 Answers

Best answer
8 votes
8 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.
edited by
1 votes
1 votes
foo will return 11

because 10 &10 will return 10

and logical OR will return 10I1 means 10+1=11
1 votes
1 votes

10 bitwise-AND 10 bitwise-OR 1

=> 1010 AND 1010 OR 0001 in binary

=> 1010 OR 0001

=> 1011

=> 11 in decimal.

Answer:

Related questions

3 votes
3 votes
4 answers
1
Arjun asked Oct 18, 2016
775 views
What will be the output of the following code?#include <stdio.h int main() { char a = 'A', z = 'Z'; printf("%d", z-a); }
7 votes
7 votes
2 answers
2
Arjun asked Oct 18, 2016
1,198 views
The value returned by the following function for foo(10) is ____int foo(int x) { if(x < 1) return 1; int sum = 0; for(int i = 1; i <= x; i++) { sum += foo(x-i); } return ...
2 votes
2 votes
5 answers
3
Arjun asked Oct 18, 2016
1,576 views
The value returned by the following code is _____int foo() { int a[] = { 10, 20, 30, 40, 50, 60 }; int *p = &a , *q = &a[5] ; return q-p; }
12 votes
12 votes
1 answer
4