@Arun: A multiple of 8 has three rightmost bits = 0. I first though about anding out these bits and adding 8. Code for that would be answer = (n & (~7)) + 8.
Alternatively, round up. That you can do by adding 8 and then zeroing out the low-order three bits. Code for that would be answer = ((n + 8) & (~7)). Another way to round up is by making the low-order three bits 1s and then adding 1. The addition will change the three low-order bits to 0s and add one to the next bit position. That's what I decided to submit since it is slightly simpler (3 characters) shorter: answer = (n | 7) + 8. Dave On Sep 29, 6:57 am, Arun <yourarunb...@gmail.com> wrote: > @Dave > > your solution is really good. > It would be helpful if you could just share how you reached at > solution. > > Cheers > Arun > On Sep 26, 4:48 pm, Dave <dave_and_da...@juno.com> wrote: > > > > > @Ashwath: Thanks for the correction. > > > Dave > > > On Sep 26, 1:20 am, aswath G B <aswat...@gmail.com> wrote: > > > > @Dave > > > > Slight change u have to do > > > > #include<stdio.h> > > > main() > > > { > > > int a = 24; > > > int b = (a | 7)+1; //This Line U have to change.....& not a || 7..... > > > printf("%d",b); > > > return 0; > > > > } > > > > tats it.... > > > > btw it is nice one line easy soln... > > > > 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. > > > > --http://aswath78.blogspot.com-Hidequoted text - > > > > - Show quoted text -- 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. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.