Computer Chess Club Archives


Search

Terms

Messages

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

Author: Dann Corbit

Date: 19:35:03 03/22/99


/*
** 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



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.