Skip to content

Commit

Permalink
add bitScanReverse; bitboardRooks
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpapado committed Apr 7, 2020
1 parent e6d8382 commit 539ac33
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 87 deletions.
66 changes: 0 additions & 66 deletions Bitboard.cpp

This file was deleted.

69 changes: 49 additions & 20 deletions bitboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

/*bitwise operations*/
namespace bitwise {
int BitScanForward(U64 bb) {
/**
* returns the index of LSB between [0,63]
* adapted by CPW, original by @author Kim Walisch (2012)
*/
if (!bb) throw std::domain_error("Bitboard is empty");
const int index64[64] = {

const int index64[64] = {
0, 47, 1, 56, 48, 27, 2, 60,
57, 49, 41, 37, 28, 16, 3, 61,
54, 58, 35, 52, 50, 42, 21, 44,
Expand All @@ -19,44 +14,77 @@ int BitScanForward(U64 bb) {
13, 18, 8, 12, 7, 6, 5, 63
};

int bitScanForward(U64 bb) {
/**
* returns the index of LSB between [0,63]
* adapted by CPW, original by @author Kim Walisch (2012)
*/
if (!bb) throw std::domain_error("Bitboard is empty");

const U64 debruijn64 = C64(0x03f79d71b4cb0a89);
return index64[((bb ^ (bb-1)) * debruijn64) >> 58];
}

int bitScanReverse(U64 bb) {
/**
* returns the index of MSB between [0,63]
* adapted by CPW, original by @author Kim Walisch (2012)
*/
if (!bb) throw std::domain_error("Bitboard is empty");

bb |= bb >> 1;
bb |= bb >> 2;
bb |= bb >> 4;
bb |= bb >> 8;
bb |= bb >> 16;
bb |= bb >> 32;
const U64 debruijn64 = C64(0x03f79d71b4cb0a89);
return index64[(bb * debruijn64) >> 58];
}


std::list<int> extractIndexes(U64 bb) {
std::list<int> myList;
while (bb) {
myList.push_back(BitScanForward(bb));
myList.push_back(bitScanForward(bb));
bb &= bb-1; //reset LSB
}
return myList;
}

void printBitboard(std::ostream &out, U64 bb){
for (int rank = 7; rank >= 0; rank--) {
for (int file = 0; file < 8; file++) {
int index = rank*8+file;
if (bb & (1ULL << index)) out << "1 ";
else out << "0 ";
}
out << "\n";
}
}

} //namespace bitwise


Bitboard::Bitboard(U64 dec) {
bb = dec;
}

int Bitboard::BitScanForward() {
return bitwise::BitScanForward(bb);
int Bitboard::bitScanForward() {
return bitwise::bitScanForward(bb);
}

int Bitboard::bitScanReverse() {
return bitwise::bitScanReverse(bb);
}

void Bitboard::printNumber() const{
void Bitboard::printNumber() const {
std::cout << bb;
return;
}

std::ostream& operator<<(std::ostream &out, const Bitboard board){
for (int rank = 7; rank >= 0; rank--) {
for (int file = 0; file < 8; file++) {
int index = rank*8+file;
if (board.bb & (1ULL << index)) out << "1 ";
else out << "0 ";
}
out << "\n";
}
bitwise::printBitboard(out, board.bb);
return out;
}

Expand All @@ -70,8 +98,9 @@ Bitboard Bitboard::getComplement() const {

void Bitboard::bitToggle(int index) {
bb ^= (1ULL << index);
return ;
return;
}

std::list<int> Bitboard::extractIndexes() const {
return bitwise::extractIndexes(bb);
}
3 changes: 2 additions & 1 deletion bitboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Bitboard {
friend std::ostream& operator<<(std::ostream &out, Bitboard bit); //prints as chessboard
void printNumber() const; //prints number
bool empty() const;
int BitScanForward();
int bitScanForward();
int bitScanReverse();
protected:
U64 bb; //bitboard 64bit
static const U64 notAFile = 0xfefefefefefefefe;
Expand Down

0 comments on commit 539ac33

Please sign in to comment.