You REALLY want to use a math library function when it is called that many times? This is trivial and is FAR faster:Henk wrote:Yes I used:Rein Halbersma wrote:In fact, the log base 2 is equal to the msbhgm wrote:Oops, you are right. I ignored that N bits are done in parallel. And without that it would of course just be O(N), as you would simply scan.
Code: Select all
public class BitBoardBackwardIterator: IBitBoardIterator { UInt64 bits; public void Reset(UInt64 bits) { this.bits = bits; } public UInt64 Next() { while (bits != 0) { int index = (int)Math.Log(bits, 2); UInt64 bit = ((UInt64)1 << index); bits &= ~bit; return bit; } return 0; } }
bits &= ~(1 << MSB(bits));
Or even better
bits ^= 1 << MSB(bits);
MSB is one instruction, shift is 1 instruction, complement is one instruction, and AND is one instruction. You will blow more than that just doing the procedure call and return.
There are fast ways to do these things. There are slow ways to do 'em. And then there are UNTHINKABLE ways of doing 'em. Little point in moving to bit boards if you are going to make everything 100x slower than it needs to be. BSF/BSR have been around since the original Intel pentium processor.
