-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitboards.go
59 lines (52 loc) · 1.36 KB
/
bitboards.go
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
package main
import "fmt"
// getBit is a function to get a bit on a certain square.
func getBit(bitboard uint64, square int) (rgw uint64) {
if bitboard&(1<<square) != 0 {
rgw = 1
}
return rgw
}
// setBit sets a bit on a certain square 1.
func setBit(bitboard uint64, square int) uint64 {
return bitboard | (1 << square)
}
// popBit sets a bit on a certain square from 1 to 0.
func popBit(bitboard uint64, square int) uint64 {
return bitboard & ^(1 << square)
}
// getLS1BIndex returns the index of the least significant bit.
func getLS1BIndex(bitboard uint64) int {
rgw := -1
if bitboard != 0 {
rgw = popCount((bitboard & -bitboard) - 1)
}
return rgw
}
// popCount counts the bits in a bitboard.
func popCount(bitboard uint64) (count int) {
if bitboard != 0 && (bitboard&bitboard-1 == 0) {
count = 1
} else if bitboard != 0 {
for bitboard != 0 {
count++
bitboard &= bitboard - 1
}
}
return count
}
// printBitboard is a debug function to print every bit in a bitboard in a 8x8 square.
func printBitboard(bitboard uint64) {
fmt.Printf(" Bitboard %d:\n", bitboard)
for rank := 0; rank < 8; rank++ {
for file := 0; file < 8; file++ {
if file == 0 {
fmt.Printf("\033[31m %d\033[0m", 8-rank)
}
square := rank*8 + file
fmt.Printf(" %d", getBit(bitboard, square))
}
fmt.Printf("\n")
}
fmt.Println("\033[31m", " A B C D E F G H\033[0m")
}