Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Peanut head {me} still does not get it. Bitboard puzzles

Author: Robert Hyatt

Date: 05:11:36 03/23/99

Go up one level in this thread


On March 22, 1999 at 22:35:03, Dann Corbit wrote:

>/*
>** Why not just do something like this?
>** How are bitboards faster?
>*/
>#include <assert.h>
>
>typedef struct tag_bitboard
>  {
>    char rows[8];
>  } bitboard;
>
>void
>SetSquare (bitboard * b, int row, int col)
>{
>#ifdef UNIT_TEST
>  assert (b);
>  assert (row >= 0 && row <= 7);
>  assert (col >= 0 && col <= 7);
>#endif
>  b->rows[row] |= (1 << col);
>}
>void
>FlipSquare (bitboard * b, int row, int col)
>{
>#ifdef UNIT_TEST
>  assert (b);
>  assert (row >= 0 && row <= 7);
>  assert (col >= 0 && col <= 7);
>#endif
>  b->rows[row] ^= (1 << col);
>}
>void
>ClearSquare (bitboard * b, int row, int col)
>{
>#ifdef UNIT_TEST
>  assert (b);
>  assert (row >= 0 && row <= 7);
>  assert (col >= 0 && col <= 7);
>#endif
>  b->rows[row] &= ~(1 << col);
>}
>int
>GetSquare (bitboard b, int row, int col)
>{
>#ifdef UNIT_TEST
>  assert (row >= 0 && row <= 7);
>  assert (col >= 0 && col <= 7);
>#endif
>  return (b.rows[row] >> (col) & 1);
>}
>
>#ifdef UNIT_TEST
>#include <stdio.h>
>void
>ShowBoard (bitboard b)
>{
>  int i;
>  int j;
>  for (i = 0; i < 8; i++)
>    {
>      for (j = 0; j < 8; j++)
>        if (GetSquare (b, i, j))
>          putchar ('*');
>        else
>          putchar ('.');
>      putchar ('\n');
>    }
>  putchar ('\n');
>}
>#endif
>
>#ifdef UNIT_TEST
>int
>main (void)
>{
>  bitboard bb = {0};
>  bitboard z = bb;
>  int i;
>
>  for (i = 0; i < 8; i++)
>    SetSquare (&bb, i, i);
>  ShowBoard (bb);
>
>  bb = z;
>  for (i = 0; i < 8; i++)
>    SetSquare (&bb, i, 0);
>  ShowBoard (bb);
>
>  bb = z;
>  for (i = 0; i < 8; i++)
>    SetSquare (&bb, 0, i);
>  ShowBoard (bb);
>  return 0;
>}
>#endif


Bitboards are faster because on 64 bit machines, they update that thing in
_one instruction_.  IE to update the from and to squares takes _one_ exclusive
or instruction for each of the 4 rotated boards.  _very dense_ information
content like that...




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.