[bitboard] Implement BitBoard::population_count()

Uses u64::count_ones() to return the population count of the BitBoard.
This commit is contained in:
Eryn Wells 2024-02-01 08:44:25 -08:00
parent 7b97060ba2
commit deea23352b

View file

@ -80,6 +80,20 @@ impl BitBoard {
!(self & square_bitboard).is_empty()
}
/// The number of 1 bits in the BitBoard.
///
/// ## Examples
///
/// ```
/// use chessfriend_bitboard::BitBoard;
/// assert_eq!(BitBoard::EMPTY.population_count(), 0);
/// assert_eq!(BitBoard::new(0b01011110010).population_count(), 6);
/// assert_eq!(BitBoard::FULL.population_count(), 64);
/// ```
pub fn population_count(&self) -> u32 {
self.0.count_ones()
}
pub fn set_square(&mut self, sq: Square) {
let sq_bb: BitBoard = sq.into();
*self |= sq_bb