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
Explain what are bus errors, memory faults, and core dumps?
Write a code of a general series where the next element is the sum of last k terms.
What is 2c dna?
hai iam working in sap sd module for one year and working in lumax ind ltd in desp department but my problem is i have done m.b.a in hr/marketing and working sap sd there is any combination it. can you give right solution of my problem. and what can i do?
Hai,I have done with my bachelor of commerce and planing to ms,please suggest me how to convince vo for shifting from commerce to computers. Visa on 8 DEC 2014 Npu university
write a program that will open the file, count the number of occurences of each word in the the complete works of shakespeare. You will then tabulate this information in another file.
What are different storage class specifiers in c?
Can you explain what keyboard debouncing is, and where and why we us it? please give some examples
Are local variables initialized to zero by default in c?
What is the difference between a function and a method in c?
Stimulate calculator using Switch-case-default statement for two numbers
What is the best organizational structure?
What is structure data type in c?
What is 2 d array in c?
What do header files do?