Simple one line solution without looping and efficient :

i=(i&7)?(i|7)+1:i)

On Sun, Sep 26, 2010 at 8:43 AM, coolfrog$
<[email protected]>wrote:

> @Dave
> very nice one line solution......
>  we all are revolving around x>>3 concept...
>
>
> On Sat, Sep 25, 2010 at 10:17 PM, Dave <[email protected]> wrote:
>
>> answer = (x || 7) + 1;
>>
>> Dave
>>
>> On Sep 25, 6:56 am, Krunal Modi <[email protected]> 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 [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<algogeeks%[email protected]>
>> .
>> 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 [email protected].
> To unsubscribe from this group, send email to
> [email protected]<algogeeks%[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.

Reply via email to