one left shift is equivalent to multiplying with 2.Two left is equivalent to multiplying with 2^2. and so on. so i left shift means multiplying with 2^i.
In your program you did left shift with 2.so it is equivalent to multiplying with 4. so when input is 1 function will return 4*1+1=5. when input is 2..output is 2*4+2=10.For 3 o/p is 3*4+3=15 On Fri, Jun 8, 2012 at 5:46 AM, Mad Coder <[email protected]> wrote: > The following is a simple C program which tries to multiply an integer by > 5 using the bitwise operations. But it doesn't do so. Explain the reason > for the wrong behavior of the program. > > #include <stdio.h> > #define PrintInt(expr) printf("%s : %d\n",#expr,(expr)) > *int* FiveTimes(*int* a) > { > *int* t; > > t *=* a*<<*2 *+* a; > > *return* t; > } > > *int* main() > { > *int* a *=* 1, b *=* 2,c *=* 3; > > PrintInt(FiveTimes(a)); > PrintInt(FiveTimes(b)); > > PrintInt(FiveTimes(c)); > *return* 0; > } > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
