Author: Jaime Benito de Valle Ruiz
Date: 06:08:54 04/17/03
Go up one level in this thread
On April 17, 2003 at 07:45:37, Evgenij84 wrote:
>i have a bitboard where only one bit is set to 1
>
>is there any function which can discover which bit is set?
I don't know any C function that can do this, but there are two in assembler:
BSR (BitScanReverse)
BSF (BitForwardReverse)
They will return the position of the first bit set to 1 that they will find, but
in a normal AMD/Pentium they only work with 32 bits.
E.g.
0000000000010 will return 1
0000100000000 will return 8
The speed of this instruction depends on how quick it can find that bit, so if
the bit is likely to be at the beginning, you better use BSF, if it is likely to
be at the end, use BSR.
This function is assembler could do the trick:
-----------------------------------------------------------
__int64 x;
int y;
__asm{
mov eax,dword ptr[x+4]
mov ebx,dword ptr[x]
bsf ecx,eax
add ecx,32
bsr ecx,[x]
mov [y],ecx
}
printf("The bit set to 1 is the bit No. %d",y);
-----------------------------------------------------------
Another (faster) alternative if check first if one of the two 32bit registers is
set to 0, and then skip the BSF/BSR for that register, and do it only for the
other one, avoiding using a potentially slow BitScan. The "drawback" is that you
have to use labels for the conditional jump.
However, you could always create a C function for it and simply return the value
I am not an expert in assembler, so I could have made a couple of mistakes in my
explanations (hope not).
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.