next higher number with same bits set

104 views
Skip to first unread message

CH Gowri Kumar - గౌరీ కుమార్

unread,
Sep 16, 2008, 8:00:59 AM9/16/08
to programmi...@googlegroups.com
Hi all,
Please find the ppt attached explaining the algorithm/solution to the
following problem:

Given a number n, finding the next higher number r, after a given
number that has the same
number of 1-bits

Please feel free to comment if the ppt is useful or otherwise :-)

Regards,
Gowri Kumar

bits_next_higher.ppt

Dave

unread,
Sep 17, 2008, 8:55:43 AM9/17/08
to Programming Puzzles
Or, for a real hacker's delight, you could just write

unsigned snoob(unsigned x) {
return (x+(x&-x))|(((x^(x+(x&-x)))>>2)/(x&-x))
}

Dave

On Sep 16, 7:00 am, "CH Gowri Kumar - గౌరీ కుమార్"
>  bits_next_higher.ppt
> 847KViewDownload
Reply all
Reply to author
Forward
0 new messages