Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Need help with nearest bit algorithm

Author: Jaime Benito de Valle Ruiz

Date: 12:11:42 01/10/04

Go up one level in this thread


On January 10, 2004 at 14:28:58, Chris Hull wrote:

>I am trying to find a fast algorithm that will return the closest bit to a given
>bit. I searched google but found nothing.
>
>Here is an example problem stated as concisely as I can.
>
>Given:
>
>    111111
>    5432109876543210    // bit positions
>
>X = 0101000100000101    // binary
>Postion = 8
>
>/** Here is the problem we are trying to solve
>    Find nearest bit higher than bit number 8 and
>    find nearest bit lower than bit number 8 **/
>
>X' = 0101000*00000101
>
>so if
>
>Y = int FindNearestHigherBit( int X, int Position );
>Z = int FindNearestLowerBit( int X, int Position );
>
>then
>
>Y = 0001000000000000
>Z = 0000000000000100
>
>So I am looking for the function/algorithm for FindNearestHigherBit and
>FindNearestLowerBit. I would like one that runs fast (ie one clock cycle)
>and not a loop that looks at every bit.
>
>Ok, all you who were CS major in college help me out. Thanks in advance.
>
>
>Chris



You could use something like this for FindNearestHigherBit:

FindNearestHigherBit(int X, int P){
   register int temp=X&(-2<<P);
   return temp&~(temp-1);
}

Maybe for can find another one faster, but at least it's something.
Let me think about the other one, and I'll post it later.
Regards,

  Jaime



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.