Mmmm, may be I understand a way to do it!
For white pawns left captures:
((((WhitePawns AND File1) XOR WhitePawns) shl 7) AND BlackPieces) give me the bitboard with all the "to"
(((((WhitePawns AND File1) XOR WhitePawns) shl 7) AND BlackPieces) shr7) give me the bitboard with all the "from"
Extracting bit by bit from each bitboard:
The 1st bits of the "to" bitboard and of the "from" bitboard are correspondings "to" and "from".
The 2nd bits of the "to" bitboard and of the "from" bitboard are correspondings "to" and "from".
...
The last bits of the "to" bitboard and of the "from" bitboard are correspondings "to" and "from".
Could I be right?
That correspondence is what I missed till now I think!
Question about the way to use bitboards.
Moderators: hgm, Rebel, chrisw
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina
-
- Posts: 243
- Joined: Sat Mar 11, 2006 8:31 am
- Location: Malmö, Sweden
- Full name: Bo Persson
Re: Question about the way to use bitboards.
Yes, except that you don't have to extract bits from both bitboards. When you have a "to" square, you know which square the "from" square must have been. So it can be computed instead of extracted (from = to + x, where x is some constant related to the shift value 7).Luis Babboni wrote: ↑Fri Mar 26, 2021 3:28 am Mmmm, may be I understand a way to do it!
For white pawns left captures:
((((WhitePawns AND File1) XOR WhitePawns) shl 7) AND BlackPieces) give me the bitboard with all the "to"
(((((WhitePawns AND File1) XOR WhitePawns) shl 7) AND BlackPieces) shr7) give me the bitboard with all the "from"
Extracting bit by bit from each bitboard:
The 1st bits of the "to" bitboard and of the "from" bitboard are correspondings "to" and "from".
The 2nd bits of the "to" bitboard and of the "from" bitboard are correspondings "to" and "from".
...
The last bits of the "to" bitboard and of the "from" bitboard are correspondings "to" and "from".
Could I be right?
That correspondence is what I missed till now I think!
-
- Posts: 27792
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Question about the way to use bitboards.
Well, start by learning C then. It is a lot simpler than English!Luis Babboni wrote: ↑Fri Mar 26, 2021 2:43 amBut part, important part, of my problem is that I do not know C and in this special case Im very bad listening english
I´ll take a look anyway.
-
- Posts: 4052
- Joined: Thu May 15, 2008 9:57 pm
- Location: Berlin, Germany
- Full name: Sven Schüle
Re: Question about the way to use bitboards.
I would rename "File1" into "FileA" but that is a matter of taste.Luis Babboni wrote: ↑Fri Mar 26, 2021 3:28 am For white pawns left captures:
((((WhitePawns AND File1) XOR WhitePawns) shl 7) AND BlackPieces) give me the bitboard with all the "to"
More important, the expression above is wrong, it should be:
(((WhitePawns AND NOT FileA) shl 7) AND BlackPieces) give me the bitboard with all the "to"
All bits belonging to the A-file must be cleared before shifting, and also the additional XOR does not make sense, it would flip a lot of bits.
Re: bitboard serialization - you do that as follows (some example pseudo code):
Code: Select all
function BitScanForwardWithReset(VAR bbX: UINT64) returns INT
{
# get the position of the LSB (least significant bit) of bbX
INT sq = BitScanForward(bbX)
# clear that LSB in bbX (therefore bbX is a parameter that gets modified!)
bbX = bbX AND (bbX - 1)
# return the LSB position which is usually a square number
return sq
}
# example 1: generate white pawn captures to the left
UINT64 bbTo = ((bbWhitePawns AND NOT BBFileA) shl 7) AND bbBlackPieces
while (bbTo NOT_EQUAL 0)
{
INT sqTo = BitScanForwardWithReset(bbTo)
INT sqFrom = sqTo - 7
GeneratePawnCapture(sqFrom, sqTo)
}
# example 2: generate all black knight moves (captures and quiet moves)
UINT64 bbFrom = bbBlackKnights
# outer loop over all squares occupied by black knights
while (bbFrom NOT_EQUAL 0)
{
# get the next "from" square
INT sqFrom = BitScanForwardWithReset(bbFrom)
# get all related "to" squares (we need to exclude squares occupied by friendly pieces)
UINT64 bbTo = GetBitboardOfAllEverPossibleKnightMovesFrom(sqFrom) AND NOT bbBlackPieces
# inner loop over all "to" squares
while (bbTo NOT_EQUAL 0)
{
# get the next "to" square for the current knight
INT sqTo = BitScanForwardWithReset(bbTo)
GenerateKnightMove(sqFrom, sqTo)
}
}
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina
Re: Question about the way to use bitboards.
Nice!Bo Persson wrote: ↑Fri Mar 26, 2021 8:45 am Yes, except that you don't have to extract bits from both bitboards. When you have a "to" square, you know which square the "from" square must have been. So it can be computed instead of extracted (from = to + x, where x is some constant related to the shift value 7).
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina
Re: Question about the way to use bitboards.
Seems the intelligent thing.hgm wrote: ↑Fri Mar 26, 2021 11:22 amWell, start by learning C then. It is a lot simpler than English!Luis Babboni wrote: ↑Fri Mar 26, 2021 2:43 amBut part, important part, of my problem is that I do not know C and in this special case Im very bad listening english
I´ll take a look anyway.
But I just dedicate to programming sporadically and the few times I tried it, when I returned to it a couple of years later, I forgot all
I think is cause is less near to a common language than Basic, and cause I learned Basic being a child.
Plus that, I think that not knowing it, protect myself to direct copy of code. And made my own, even awful, code is the real fun of this, not the level my chess engine could reach.
I adviced, this I told is not the intelligence thing.
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina
Re: Question about the way to use bitboards.
Yes, must be FileA!Sven wrote: ↑Fri Mar 26, 2021 11:25 amI would rename "File1" into "FileA" but that is a matter of taste.Luis Babboni wrote: ↑Fri Mar 26, 2021 3:28 am For white pawns left captures:
((((WhitePawns AND File1) XOR WhitePawns) shl 7) AND BlackPieces) give me the bitboard with all the "to"
More important, the expression above is wrong, it should be:
(((WhitePawns AND NOT FileA) shl 7) AND BlackPieces) give me the bitboard with all the "to"
All bits belonging to the A-file must be cleared before shifting, and also the additional XOR does not make sense, it would flip a lot of bits.
...
I still think my code is right, but your suggest is shorter (so better)!
EDIT: not sure if yours is shorter.
What is shorter:
BB1 AND BB2 XOR BB2
or
BB1 AND NOT BB2?
Last edited by Luis Babboni on Fri Mar 26, 2021 12:43 pm, edited 1 time in total.
-
- Posts: 4052
- Joined: Thu May 15, 2008 9:57 pm
- Location: Berlin, Germany
- Full name: Sven Schüle
Re: Question about the way to use bitboards.
EDIT: You are right regarding the result ... but hey ... go the direct way instead of cryptographyLuis Babboni wrote: ↑Fri Mar 26, 2021 12:39 pmYes, must be FileA!Sven wrote: ↑Fri Mar 26, 2021 11:25 amI would rename "File1" into "FileA" but that is a matter of taste.Luis Babboni wrote: ↑Fri Mar 26, 2021 3:28 am For white pawns left captures:
((((WhitePawns AND File1) XOR WhitePawns) shl 7) AND BlackPieces) give me the bitboard with all the "to"
More important, the expression above is wrong, it should be:
(((WhitePawns AND NOT FileA) shl 7) AND BlackPieces) give me the bitboard with all the "to"
All bits belonging to the A-file must be cleared before shifting, and also the additional XOR does not make sense, it would flip a lot of bits.
...
I still think my code is right, but your suggest is shorter (so better)!
Last edited by Sven on Fri Mar 26, 2021 12:48 pm, edited 1 time in total.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina
Re: Question about the way to use bitboards.
Let me see agian then. Sorry.Sven wrote: ↑Fri Mar 26, 2021 12:42 pmI explained why it was wrongLuis Babboni wrote: ↑Fri Mar 26, 2021 12:39 pmYes, must be FileA!Sven wrote: ↑Fri Mar 26, 2021 11:25 amI would rename "File1" into "FileA" but that is a matter of taste.Luis Babboni wrote: ↑Fri Mar 26, 2021 3:28 am For white pawns left captures:
((((WhitePawns AND File1) XOR WhitePawns) shl 7) AND BlackPieces) give me the bitboard with all the "to"
More important, the expression above is wrong, it should be:
(((WhitePawns AND NOT FileA) shl 7) AND BlackPieces) give me the bitboard with all the "to"
All bits belonging to the A-file must be cleared before shifting, and also the additional XOR does not make sense, it would flip a lot of bits.
...
I still think my code is right, but your suggest is shorter (so better)!
-
- Posts: 4052
- Joined: Thu May 15, 2008 9:57 pm
- Location: Berlin, Germany
- Full name: Sven Schüle
Re: Question about the way to use bitboards.
You can express 12 as 3*4 or as 3*(sqrt(64)/2) ...Luis Babboni wrote: ↑Fri Mar 26, 2021 12:39 pmYes, must be FileA!Sven wrote: ↑Fri Mar 26, 2021 11:25 amI would rename "File1" into "FileA" but that is a matter of taste.Luis Babboni wrote: ↑Fri Mar 26, 2021 3:28 am For white pawns left captures:
((((WhitePawns AND File1) XOR WhitePawns) shl 7) AND BlackPieces) give me the bitboard with all the "to"
More important, the expression above is wrong, it should be:
(((WhitePawns AND NOT FileA) shl 7) AND BlackPieces) give me the bitboard with all the "to"
All bits belonging to the A-file must be cleared before shifting, and also the additional XOR does not make sense, it would flip a lot of bits.
...
I still think my code is right, but your suggest is shorter (so better)!
EDIT: not sure if yours is shorter.
What is shorter:
BB1 AND BB2 XOR BB2
or
BB1 AND NOT BB2?
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)