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 / jaroosh

This is a very neat example of program where you need to
note some interesting things about values in order to easily
estimate how many times it will be executed.
So, here it goes
0. first of all, were looking for the value of LOOP for
which (value >> 1) & 1 | loop & 1 will be false.
1. loop variable in for loop will sequentially take values
that are :
odd (1), even (2), odd (3), even (4) ...etc
so (loop & 1) part of for loop condition will sequentially
take values:
true(loop=1), false(loop=2),true,false...etc
2. the only thing to figure now is how (value >> 1) & 1 part
will behave. It is a good choice to convert hex abcd to bit
pattern, so we will have 1010101111001101
now only thing we have to do is see what will the boolean
part of condition will initially be:
1010101111001101 >> 1 = 101010111100110
101010111100110 & 1 = 0
initially, the (value >> 1) & 1 part is then FALSE.
3. now, we only have see 3 things:
a) in for loop body there is :
if (loop & 1)
value >>= 1;
so EVERY TIME loop condition : (loop & 1) is TRUE (as we've
noticed earlier for each loop iteration, it has sequentially
values : true, false, true, false, true, false..etc),
we CHANGE the value of '(value >> 1) & 1' part of condition
to its opposite (if it was FALSE, it becomes true)
b) were looking for the situation where both parts of
condition will be false, so all we have to do now is write
down how those parts behave starting from loop = 1.

LOOP PART : true , false , true , false , true , false(!)
OTHER PART: false , true, true, true, false , FALSE(!)

since we have FALSE and FALSE in both parts of for
condition, the for loop ends.
Now, we count how many times the for loop body (and so - foo
function) executed. Right, it IS 5.

It may sound a bit complicated, but this shows the way how
to solve algorithm problems that sound complicated at first
and use bit patterns (on interviews, no-one expects you to
do complicated binary/hex equatations in memory, you only
have to note some things, and simplify).

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the full form of getch?

858


What does nil mean in c?

883


What is getch () for?

878


What is unsigned int in c?

733


Describe explain how arrays can be passed to a user defined function

841


write a c programming using command line argument,demonstrate set operation(eg;union,intersection,difference) example output is c:>setop 12 34 45 1 union 34 42 66 c:>setop 12 34 1 42 66 c:>setop 12 34 diff 12 56 67 78 setop 12 34

1839


‘SAVEPOINT’ and ‘ROLLBACK’ is used in oracle database to secure the data comment. Give suitable examples of each with sql command.

2074


What is the difference between #include and #include 'file' ?

804


How do you generate random numbers in C?

893


Hai what is the different types of versions and their differences

1713


If you know then define #pragma?

860


How do you list files in a directory?

794


What does != Mean in c?

785


What does #pragma once mean?

908


Why is event driven programming or procedural programming, better within specific scenario?

2154