-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bitboard.cpp
122 lines (100 loc) · 2.43 KB
/
Bitboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "Bitboard.h"
#include <cstdint>
#include <bit>
#include <iostream>
Bitboard::Bitboard() {
m_board = 0;
}
Bitboard::Bitboard(uint64_t val) {
m_board = val;
}
Bitboard Bitboard::operator= (Bitboard op) {
m_board = op.getBits();
return m_board;
}
bool Bitboard::operator== (Bitboard op) {
return m_board == op.getBits();
}
bool Bitboard::operator!= (Bitboard op) {
return m_board != op.getBits();
}
Bitboard Bitboard::operator>>= (int op) {
m_board >>= op;
return m_board;
}
Bitboard Bitboard::operator>> (int op) {
return m_board >> op;
}
Bitboard Bitboard::operator<<= (int op) {
m_board <<= op;
return m_board;
}
Bitboard Bitboard::operator<< (int op) {
return m_board << op;
}
Bitboard Bitboard::operator^= (Bitboard op) {
m_board ^= op.getBits();
return m_board;
}
Bitboard Bitboard::operator^ (Bitboard op) {
return m_board ^ op.getBits();
}
Bitboard Bitboard::operator&= (Bitboard op) {
m_board &= op.getBits();
return m_board;
}
Bitboard Bitboard::operator& (Bitboard op) {
return m_board & op.getBits();
}
Bitboard Bitboard::operator|= (Bitboard op) {
m_board |= op.getBits();
return m_board;
}
Bitboard Bitboard::operator| (Bitboard op) {
return m_board | op.getBits();
}
Bitboard Bitboard::operator*= (Bitboard op) {
m_board |= op.getBits();
return m_board;
}
Bitboard Bitboard::operator* (Bitboard op) {
return m_board * op.getBits();
}
Bitboard Bitboard::operator~ () {
return ~m_board;
}
int Bitboard::getLsb() {
if(m_board == 0) return -1;
uint64_t bb = m_board & -m_board; // extract the lsb
// binary search to find index of the bit
// GROUP B SKILL: binary search
int index = 0;
if(bb>>32) { bb>>=32; index+=32; }
if(bb>>16) { bb>>=16; index+=16; }
if(bb>>8) { bb>>=8; index+=8; }
if(bb>>4) { bb>>=4; index+=4; }
if(bb>>2) { bb>>=2; index+=2; }
if(bb>>1) { bb>>=1; index++; }
return index;
}
int Bitboard::popLsb() {
if(m_board == 0) return -1;
uint64_t bb = m_board & -m_board; // extract the lsb
m_board ^= bb; // wipe the bit
// binary search to find index of the bit
// GROUP B SKILL: binary search
int index = 0;
if(bb>>32) { bb>>=32; index+=32; }
if(bb>>16) { bb>>=16; index+=16; }
if(bb>>8) { bb>>=8; index+=8; }
if(bb>>4) { bb>>=4; index+=4; }
if(bb>>2) { bb>>=2; index+=2; }
if(bb>>1) { bb>>=1; index++; }
return index;
}
int Bitboard::popcnt() {
return std::__popcount(m_board);
}
uint64_t Bitboard::getBits() {
return m_board;
}