Question about the way to use bitboards.

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Luis Babboni
Posts: 464
Joined: Sat Feb 28, 2015 4:37 pm
Location: Argentina

Re: Question about the way to use bitboards.

Post by Luis Babboni »

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!
User avatar
Bo Persson
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.

Post by Bo Persson »

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!
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).
User avatar
hgm
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.

Post by hgm »

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.
Well, start by learning C then. It is a lot simpler than English! :lol:
Sven
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.

Post by Sven »

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"
I would rename "File1" into "FileA" but that is a matter of taste.

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)
User avatar
Luis Babboni
Posts: 464
Joined: Sat Feb 28, 2015 4:37 pm
Location: Argentina

Re: Question about the way to use bitboards.

Post by Luis Babboni »

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).
Nice!
User avatar
Luis Babboni
Posts: 464
Joined: Sat Feb 28, 2015 4:37 pm
Location: Argentina

Re: Question about the way to use bitboards.

Post by Luis Babboni »

hgm wrote: Fri Mar 26, 2021 11:22 am
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.
Well, start by learning C then. It is a lot simpler than English! :lol:
Seems the intelligent thing.
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 :oops:
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. :D
I adviced, this I told is not the intelligence thing. :lol:
User avatar
Luis Babboni
Posts: 464
Joined: Sat Feb 28, 2015 4:37 pm
Location: Argentina

Re: Question about the way to use bitboards.

Post by Luis Babboni »

Sven wrote: Fri Mar 26, 2021 11:25 am
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"
I would rename "File1" into "FileA" but that is a matter of taste.

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.
...
Yes, must be FileA! :oops:
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.
Sven
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.

Post by Sven »

Luis Babboni wrote: Fri Mar 26, 2021 12:39 pm
Sven wrote: Fri Mar 26, 2021 11:25 am
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"
I would rename "File1" into "FileA" but that is a matter of taste.

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.
...
Yes, must be FileA! :oops:
I still think my code is right, but your suggest is shorter (so better)!
EDIT: You are right regarding the result ... but hey ... go the direct way instead of cryptography 😉
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)
User avatar
Luis Babboni
Posts: 464
Joined: Sat Feb 28, 2015 4:37 pm
Location: Argentina

Re: Question about the way to use bitboards.

Post by Luis Babboni »

Sven wrote: Fri Mar 26, 2021 12:42 pm
Luis Babboni wrote: Fri Mar 26, 2021 12:39 pm
Sven wrote: Fri Mar 26, 2021 11:25 am
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"
I would rename "File1" into "FileA" but that is a matter of taste.

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.
...
Yes, must be FileA! :oops:
I still think my code is right, but your suggest is shorter (so better)!
I explained why it was wrong 🤔
Let me see agian then. Sorry.
Sven
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.

Post by Sven »

Luis Babboni wrote: Fri Mar 26, 2021 12:39 pm
Sven wrote: Fri Mar 26, 2021 11:25 am
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"
I would rename "File1" into "FileA" but that is a matter of taste.

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.
...
Yes, must be FileA! :oops:
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?
You can express 12 as 3*4 or as 3*(sqrt(64)/2) ...
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)