From b4bc91ffc22a37e771da3d1d09269d4c2ae73620 Mon Sep 17 00:00:00 2001 From: "rektdie@gmail.com" Date: Sun, 3 Mar 2024 21:28:39 +0100 Subject: [PATCH] Added popCount function to bitboards --- source/bitboards.cpp | 13 +++++++++++++ source/bitboards.h | 2 ++ source/main.cpp | 5 ++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/source/bitboards.cpp b/source/bitboards.cpp index 752970c..7867165 100644 --- a/source/bitboards.cpp +++ b/source/bitboards.cpp @@ -30,6 +30,19 @@ void Bitboard::SetBit(int square) { m_board |= (1ULL << square); } +int Bitboard::popCount() const { + U64 board = m_board; + int count = 0; + + while (board) { + board &= board - 1; + + count++; + } + + return count; +} + // Operator overloading for easier comparisons Bitboard Bitboard::operator&(const Bitboard& other) const { return m_board & other.m_board; diff --git a/source/bitboards.h b/source/bitboards.h index 5d994fa..9b3a17b 100644 --- a/source/bitboards.h +++ b/source/bitboards.h @@ -17,6 +17,8 @@ public: void SetBit(int square); void PrintBoard() const; + int popCount() const; + static Bitboard GetSquare(int square) { return (1ULL << square); }; diff --git a/source/main.cpp b/source/main.cpp index 1b1b0e2..0247e3c 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -4,11 +4,10 @@ #include "movegen.h" int main() { + initLeaperAttacks(); + Board board; board.Init(); - initLeaperAttacks(); - rookAttacksOnTheFly(e4, board.occupied).PrintBoard(); - return 0; } \ No newline at end of file