aboutsummaryrefslogtreecommitdiff
path: root/src/bitboard.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bitboard.rs')
-rw-r--r--src/bitboard.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/bitboard.rs b/src/bitboard.rs
index 6312dd8..b05803c 100644
--- a/src/bitboard.rs
+++ b/src/bitboard.rs
@@ -44,6 +44,21 @@ pub fn bitscan(bb: Bitboard) -> u8 {
pop_count(bb - 1)
}
+pub fn bitscan_and_reset(bb: &mut Bitboard) -> u8 {
+ let ls1b_bitboard = ls1b(*bb);
+ *bb ^= ls1b_bitboard;
+ bitscan(ls1b_bitboard)
+}
+
+pub fn serialize_bitboard(bb: Bitboard) -> Vec<u8> {
+ let mut serialized = Vec::with_capacity(64);
+ let mut bitboard = bb;
+ while bitboard > 0 {
+ serialized.push(bitscan_and_reset(&mut bitboard));
+ }
+ serialized
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -73,5 +88,22 @@ mod tests {
fn test_bitscan_with_non_single_bb() {
bitscan(5);
}
+
+ #[test]
+ fn test_bitscan_and_reset() {
+ assert_eq!(bitscan(4), 2);
+ assert_eq!(bitscan(16), 4);
+ assert_eq!(bitscan(64), 6);
+ assert_eq!(bitscan(128), 7);
+ }
+
+ #[test]
+ fn test_serialize_bitboard() {
+ let bb = 1 << 4 | 1 << 15 | 1 << 60;
+ let serialized = serialize_bitboard(bb);
+ assert_eq!(serialized[0], 4);
+ assert_eq!(serialized[1], 15);
+ assert_eq!(serialized[2], 60);
+ }
}