Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Binary question

Author: James Swafford

Date: 16:03:22 10/19/00

Go up one level in this thread


On October 19, 2000 at 17:09:39, Severi Salminen wrote:

>Hi!
>
>Now I have my bitboard move generator online generating pawn and knight moves!
>It works, great! One question:
>
>How do I find n in a binary number 2^n? So f(1000b)=3? Now I just shift right:
>
>c=0;
>while(B>>++c) ;
>c--;
>
>Any ideas?
>
>Severi

The easiest (and slowest) way is to count the number of right shifts.
Something like:

----------
for (n=0;n<max_bits;n++) {
   if (binary_number&1) break;
   binary_number>>=1;
}

bit_position=n;
-----------

There are faster ways, like table lookups.  Precomputation is
always faster.

--
James



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.