Chess Engine
C++ chess engine with movegen, bitboards, and Arduino-friendly docs
Loading...
Searching...
No Matches
bitboards.cpp
Go to the documentation of this file.
1// bitboards.c
2
3#include "stdio.h"
4#include "defs.h"
5#include "setup.h"
6
7const int BitTable[64] = {
8 63, 30, 3, 32, 25, 41, 22, 33, 15, 50, 42, 13, 11, 53, 19, 34, 61, 29, 2,
9 51, 21, 43, 45, 10, 18, 47, 1, 54, 9, 57, 0, 35, 62, 31, 40, 4, 49, 5, 52,
10 26, 60, 6, 23, 44, 46, 27, 56, 16, 7, 39, 48, 24, 59, 14, 12, 55, 38, 28,
11 58, 20, 37, 17, 36, 8
12};
13
14int popLSB(U64 *bb) {
15 U64 b = *bb ^ (*bb - 1);
16 unsigned int fold = (unsigned) ((b & 0xffffffff) ^ (b >> 32));
17 *bb &= (*bb - 1);
18 return BitTable[(fold * 0x783a9b23) >> 26];
19}
20
22 int r;
23 for(r = 0; b; r++, b &= b - 1);
24 return r;
25}
26
28
29 U64 shiftMe = 1ULL;
30
31 int rank = 0;
32 int file = 0;
33 int sq = 0;
34 int sq64 = 0;
35
36 printf("\n");
37 for(rank = RANK_8; rank >= RANK_1; --rank) {
38 for(file = FILE_A; file <= FILE_H; ++file) {
39 sq = FR2SQ(file,rank); // 120 based
40 sq64 = SQ64(sq); // 64 based
41
42 if((shiftMe << sq64) & bb)
43 printf("X");
44 else
45 printf("-");
46
47 }
48 printf("\n");
49 }
50 printf("\n\n");
51}
const int BitTable[64]
Lookup table for bit index extraction.
Definition bitboards.cpp:7
void printBitBoard(U64 bb)
Prints a visual representation of a bitboard to stdout.
Definition bitboards.cpp:27
int popLSB(U64 *bb)
Pops the least significant 1-bit from a bitboard and returns its index (0–63).
Definition bitboards.cpp:14
int countSetBits(U64 b)
Counts the number of 1-bits in a bitboard.
Definition bitboards.cpp:21
#define FR2SQ(f, r)
Definition defs.h:181
@ FILE_H
Definition defs.h:41
@ FILE_A
Definition defs.h:41
unsigned long long U64
Definition defs.h:26
#define SQ64(sq120)
Definition defs.h:182
@ RANK_8
Definition defs.h:42
@ RANK_1
Definition defs.h:42