Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


value = 0xabcd;
for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) {
foo();
if (loop & 1)
value >>= 1;
}

how many times is foo() executed?

Answers were Sorted based on User's Feedback



value = 0xabcd; for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) { foo..

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

value = 0xabcd; for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) { foo..

Answer / guest

5 times

Is This Answer Correct ?    2 Yes 0 No

value = 0xabcd; for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) { foo..

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

value = 0xabcd; for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) { foo..

Answer / kartik

how it can execute 5 times

Is This Answer Correct ?    2 Yes 1 No

value = 0xabcd; for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) { foo..

Answer / sbay

Shouldn't it be || though?
for (loop = 1; (value >> 1) & 1 || loop & 1; loop++)

????

Is This Answer Correct ?    0 Yes 0 No

value = 0xabcd; for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) { foo..

Answer / ismail

its not 5 its will be 7 !!!!!

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

how to print electricity bill according to following charges first 100 units -1rs per unit for next 200 units-1.50 rs per unit without using conditions

0 Answers  


write a program to convert a expression in polish notation(postfix) to inline(normal) something like make 723+* (2+3) x 7 (not sure) just check out its mainly printing expression in postfix form to infix.

0 Answers   Lovely Professional University,


#include<stdio.h> main(0 { printf("\n %d %d %d",sizeof(3),sizeof("3"),sizeof(3)); }

4 Answers   HCL,


read a number & print all its devisors using c-program?

3 Answers  


my name is nani i completed my b-tech in hyd now i want go for interveiw but i dont know the process of software field interveiws plz help me anyone how many rouds there n what rounds plz plz plz help me n where i can get these details

2 Answers  


Can you return null in c?

0 Answers  


differentiate built-in functions and user – defined functions.

0 Answers  


Can a variable be both static and volatile in c?

0 Answers  


What is the difference between malloc calloc and realloc in c?

0 Answers  


a sequence of bytes with one to one corrspondence to those in the external device a) sequential addressing b) address c) byte code d) none

0 Answers  


Explain how to reverse singly link list.

0 Answers  


Do array subscripts always start with zero?

0 Answers  


Categories