Computer Chess Club Archives


Search

Terms

Messages

Subject: fast count function for bitboards

Author: scott farrell

Date: 08:09:56 09/22/02


I have a board.Count method in my Java code, and it seems excessively slow. So
much so I had to mostly exclude it from my evaluate function.

I am coding in java I know, but I dont have any idea how this code even works (I
borrowed it from deja a few months back - it was faster than a loop of 0 to 63).

I found it was slow even counting a few bits that were on, say for counting the
number of squares a bishop could move to.

Can someone give me an opinion if these is reasonable code. Or is any sort of
counting in bitboard is fately slow.

Thanx
Scott

Here is the code:

static final long ONES = 0x5555555555555555L;
 static final long TWOS = 0x3333333333333333L;
 static final int FOURS = 0x0f0f0f0f;

public static final int count(long set) {
  set = (set & ONES) + ((set >>> 1) & ONES);
  set = (set & TWOS) + ((set >>> 2) & TWOS);
  int result = (int) (set & 0xffffffff) + (int) (set >>> 32);
  result = (result & FOURS) + ((result >>> 4) & FOURS);
  return (result * 0x01010101) >>> 24;
 }




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.