value = 0xabcd;
for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) {
foo();
if (loop & 1)
value >>= 1;
}
how many times is foo() executed?
Answer Posted / adam
It is 5, as stated... anybody can figure that out by just compiling the code. I
doubt that guy actually knows why.
This problem is heavy on bitwise operations, so you'll need to convert the
numbers into binary. The 0xABCD = 10, 11, 12, 13 = 1010 1011 1100
1101. Now make a table to hold the binary values of loop and value on each
iteration and walk through the code. Notice that "value" is shifted every other
iteration.
At the start of the 6th iteration, the value of "value" is 1010101111001 and
the "loop" is obviously 110. This makes the for-loop check:
((value >> 1) & 1) | (110 & 1)
(101010111100 & 1) | (110 & 1)
0 | 0
0
And it exits the loop at this point.
| Is This Answer Correct ? | 2 Yes | 0 No |
Post New Answer View All Answers
When should a far pointer be used?
What is the correct declaration of main?
Is there a built-in function in C that can be used for sorting data?
Can a pointer point to null?
Is main a keyword in c?
What is the difference between volatile and const volatile?
When should a type cast be used?
write a c program to calculate sum of digits till it reduces to a single digit using recursion
What are the 4 types of functions?
What is "Hungarian Notation"?
What is boolean in c?
Can you please explain the difference between exit() and _exit() function?
What is return type in c?
How to draw the flowchart for structure programs?
why wipro wase