int count1=count2=0;

given number=n;
int m=n;      //save in temp variable m

while(m!=0)   //count number of bits in given number
{
   m=m & (m-1);
   count1++;
}

m=n;  //save in temp variable m

while(count1!=count2)      //check no. of bits same or not
{
    m=m+1;                     //increment number everytime
    result=m;                   //if exits from loop then this contain
result
    count2=0;
    while(m!=0)
    {
       m=m & (m-1);
       count2++;
    }
}


On Sat, Jul 9, 2011 at 5:38 PM, Aman Goyal <[email protected]> wrote:

> Solutn:
>
> 1101000
>
> Start from rightmost bit->leftmost bit
>
> Find the starting and ending 1’s positions, here 3 and 7
>
> If any 0 bw them… while traversing.. (bitwise r->l)..  make it 1 and other
> adjacent right ” 1” as  “0”. And this is your new no. here 1110000
>
>
> If you find no “0” in bw ( eg for 111100)
>
> Then ,a bit will be increased.
>
> Make first  bit from left 1 and  set the lower order  (total_set_bits-1)
> bits..  this will be the new no.
>
> Here : 1000111.
>
>
> On Sat, Jul 9, 2011 at 2:53 PM, Piyush Sinha <[email protected]>wrote:
>
>> I found a good question to try for bit manipulation.....Try it... :)
>>
>> Given an integer x, find out the smallest integer which has same
>> number of set bits as x and is greater than x.
>>
>> For example if the input integer is 12 (1100) then your function
>> should return 17(10001). If the input integer is 3(11) then your
>> function should return 5(101)
>>
>> --
>> *Piyush Sinha*
>> *IIIT, Allahabad*
>> *+91-8792136657*
>> *+91-7483122727*
>> *https://www.facebook.com/profile.php?id=100000655377926 *
>>
>> --
>> 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.
>

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