aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bitboard.c9
-rw-r--r--src/bitboard.h4
-rw-r--r--src/main.c4
3 files changed, 15 insertions, 2 deletions
diff --git a/src/bitboard.c b/src/bitboard.c
index e9a7d53..cffe40f 100644
--- a/src/bitboard.c
+++ b/src/bitboard.c
@@ -25,6 +25,13 @@ inline Bitboard ls1b(Bitboard 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) {
+inline int bitscan(Bitboard bb) {
return pop_count(ls1b(bb) - 1);
}
+
+/* Bitscan forward with LS1B reset */
+inline int bitscanAndReset(Bitboard* bb) {
+ int idx = bitscan(*bb);
+ *bb &= *bb - 1;
+ return idx;
+}
diff --git a/src/bitboard.h b/src/bitboard.h
index b90bd8f..ebac2f7 100644
--- a/src/bitboard.h
+++ b/src/bitboard.h
@@ -9,4 +9,6 @@ void print_bitboard(Bitboard bb);
Bitboard ls1b(Bitboard bb);
-Bitboard bitscan(Bitboard bb);
+int bitscan(Bitboard bb);
+
+int bitscanAndReset(Bitboard* bb);
diff --git a/src/main.c b/src/main.c
index 2bd9583..0272073 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5,6 +5,10 @@
int main() {
start_test_section("Bitboards"); {
unit_test(pop_count(0b01110) == 3, "Pop count of 01110 is 3");
+
+ Bitboard bb = 0b1100;
+ unit_test(bitscanAndReset(&bb) == 2, "Bitscan of 0b1100 is 2");
+ unit_test(bb == 0b1000, "After bitscan with reset the LS1B is flipped");
}
start_test_section("Default FEN string"); {