@Dave very nice one line solution...... we all are revolving around x>>3 concept...
On Sat, Sep 25, 2010 at 10:17 PM, Dave <dave_and_da...@juno.com> wrote: > answer = (x || 7) + 1; > > Dave > > On Sep 25, 6:56 am, Krunal Modi <krunalam...@gmail.com> wrote: > > Q: Write an algorithm to compute the next multiple of 8 for a given > > positive integer using bitwise operators. > > > > Example, > > (a) For the number 12, the next multiple of 8 is 16. > > (b) For the number 16, the next multiple of 8 is 24. > > ------------------------- > > I have written a code using bitwise operators...but, I am not happy > > with the solution....Is there any ONE LINE solution ? > > Here, is my code. > > Approach: shift left till LSB is 0 (say n number of times) then, OR > > with 1, finally shift right (same number of times). > > > > #include<stdio.h> > > > > int main(){ > > int count,m,n,ans,t; > > > > printf("Enter any number :"); > > scanf("%d",&n); > > > > m=8; //multiple of 8 !! > > ans=0; > > while(ans<=n){ > > t = m; > > while(t){ > > count=0; > > while(ans&1){ > > count++; > > ans = ans>>1; > > } > > ans = ans|1; > > ans = ans<<count; > > t--; > > } > > } > > > > printf("[%d]\n",ans); > > > > return 0; > > > > > > > > }- Hide quoted text - > > > > - Show quoted text - > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algoge...@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- *Divesh* (¨`·.·´¨) Always `·.¸(¨`·.·´¨ ) Keep (¨`·.·´¨)¸.·´Smiling! `·.¸.·´" Life can give u 100's of reason 2cry,but u can give life 1000's of reasons 2Smile" -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.