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
What is difference between union All statement and Union?
Why is c used in embedded systems?
What are the benefits of c language?
What is the use of a ‘ ’ character?
Explain what is the difference between far and near ?
Give basis knowledge of web designing ...
Are the outer parentheses in return statements really optional?
Is it better to bitshift a value than to multiply by 2?
What is the sizeof () a pointer?
Does c have enums?
What is static volatile in c?
What is size of union in c?
How is a pointer variable declared?
Write a program to reverse a given number in c language?
What is a pointer on a pointer in c programming language?