I found an algo in hackerÅ› delight (
http://www.amazon.com/Hackers-Delight-Henry-S-Warren/dp/0201914654/ref=sr_1_1?ie=UTF8&s=books&qid=1299020349&sr=8-1)
that solve the problem of find the next number with the same number of
'ones'... may  be it can give u a direction... a naive implementation :

#include<stdio.h>

int snoob(unsigned int x){
  unsigned smallest, ripple, ones;

  smallest = x & -x;
  ripple = x + smallest;
  ones = x ^ ripple;
  ones = (ones >> 2)/smallest;
  return ripple | ones;
}

void bin(unsigned int x){
  unsigned int one = 0x80000000;
  int i;
  for(i=31; i>=0; i--){
    printf("%d", ((x&one)>>i));
    one = one >> 1;
  }
  printf("\n");
}

int main(){

  int n = 0x00000003;
  int i;

  for(i=0; i<100; i++){
    printf("%d -- ", n);
    bin(n);
    n = snoob(n);
  }

  return 0;
}



On Tue, Mar 1, 2011 at 4:50 PM, bittu <[email protected]> wrote:

> Given an integer, print the next smallest and next largest number that
> have the same number of 1 bits in their binary representation.
>
> Example for n=12
> next smallest  with same no. of set bit is 17  but  to found next
> largest ..we have to be careful ..
>
> let c others ..??
>
>
>
> Thanks & Regards
> Shashank
>
>
>
>
>
>
>
>
>
>
>
> --
> 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.
>
>


-- 
Wesley Mesquita
Computer Engineer
http://www.wesleymesquita.com
Mobile: +55 11 95249272

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