• retagged by
2,683 views
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;
}

4 Answers

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.
• edited by
1 1 vote

10 bitwise-AND 10 bitwise-OR 1

=> 1010 AND 1010 OR 0001 in binary

=> 1010 OR 0001

=> 1011

=> 11 in decimal.

Answer:
Position:
Show:

Related questions

7 7 votes
2 answers 2 answers
2.3k
2.3k views
Arjun asked Oct 18, 2016
2,316 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 ...
13 13 votes
1 answers 1 answer
3.5k
3.5k views
Arjun asked Oct 18, 2016
3,498 views
#include <stdio.h int foo(int a[100]) { return sizeof(a); } int main() { int a[10]; printf("%d", foo(a)); }What will be the output of the above code ignoring any compiler...
4 4 votes
2 answers 2 answers
2.3k
2.3k views
Arjun asked Oct 18, 2016
2,343 views
No. of times '*' will be printed by the following C code is _____#include<stdio.h void foo(int x) { switch(x){ case 1: printf("*"); case 2: printf("*"); case 3: printf("*...
7 7 votes
4 answers 4 answers
3.5k
3.5k views
Arjun asked Oct 18, 2016
3,525 views
The output of the following C program will be _____#include<stdio.h #define type int type foo(type b) { return b*b; } #undef type #define type float int main() { float a ...