aboutsummaryrefslogtreecommitdiff
path: root/src/bitboard.c
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-09-14 17:25:09 +0300
committereug-vs <eugene@eug-vs.xyz>2022-09-14 17:25:09 +0300
commit1373e30387bf5e8e7858e10c9dee4413bf20d71c (patch)
tree21ec9888a613cfe454ccdefa3de2d37854a112f1 /src/bitboard.c
parent2dfa7cecdacacf7ef84e1ca768db6d636e225a0c (diff)
downloadj1chess-1373e30387bf5e8e7858e10c9dee4413bf20d71c.tar.gz
refactor: separate bitboard functionality
Diffstat (limited to 'src/bitboard.c')
-rw-r--r--src/bitboard.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/bitboard.c b/src/bitboard.c
new file mode 100644
index 0000000..e9a7d53
--- /dev/null
+++ b/src/bitboard.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include "bitboard.h"
+
+/* Print bitboard on screen in the same way squares appear in memory
+ * (i.e the board is actually flipped along X) */
+void print_bitboard(Bitboard bb) {
+ for (U64 index = 0; index < 64; index++) {
+ printf("%lu", (bb >> index) & 1);
+ if ((index + 1) % 8 == 0) printf("\n");
+ }
+ printf("\n\n");
+}
+
+/* Return bitboard cardinality, aka number of elements in the set */
+inline int pop_count(Bitboard bb){
+ if (bb == 0) return 0;
+ return pop_count(bb >> 1) + (bb & 1);
+}
+
+/* Return Bitboard with only Least Single Bit */
+inline Bitboard ls1b(Bitboard bb) {
+ return bb & -bb;
+}
+
+/* Log base 2 (aka Trailing Zero Count)
+ * Only works for SINGLE Bitboards
+ * Useful for calculating bit-index of LS1B */
+inline Bitboard bitscan(Bitboard bb) {
+ return pop_count(ls1b(bb) - 1);
+}