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 »

Luis Babboni wrote: Fri Mar 26, 2021 12:43 pm Let me see agian then. Sorry.
I think it like this:
Image
Last edited by Luis Babboni on Fri Mar 26, 2021 1:02 pm, edited 1 time in total.
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:53 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)!

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) ...
I do not understand enough of how the operators and bits works to see why is like this, sorry.
The NOT is faster than all others?
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 1:02 pm
Sven wrote: Fri Mar 26, 2021 12:53 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)!

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) ...
I do not understand enough of how the operators and bits works to see why is like this, sorry.
The NOT is faster than all others?
That is not my point.

The goal is to clear all bits on file A.

I do this by clearing all bits on file A. (... AND NOT FileA)

You do this by clearing all other pawn bits and then flipping all pawn bits again. That is cryptography 😎
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 »

Mmmmm, may be the NOT could be faster, use just one BB, but no matter that, I could have a NOTFilaA BB stored since the engine boot. :wink:
I´m right?
Thanks!
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 1:07 pm That is not my point.

The goal is to clear all bits on file A.

I do this by clearing all bits on file A. (... AND NOT FileA)

You do this by clearing all other pawn bits and then flipping all pawn bits again. That is cryptography 😎
So is a matter of "sthetic"? (Not few thinking in a future understanding)
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 1:12 pm
Sven wrote: Fri Mar 26, 2021 1:07 pm That is not my point.

The goal is to clear all bits on file A.

I do this by clearing all bits on file A. (... AND NOT FileA)

You do this by clearing all other pawn bits and then flipping all pawn bits again. That is cryptography 😎
So is a matter of "sthetic"? (Not few thinking in a future understanding)
No, a matter of "straight forward thinking". Just don't wrap your head around how fast those bit operations are (and the difference is not that big). You are currently in a phase where you want to try to understand basic bitboard concepts. The basic concept of clearing some bits BB2 in a bitboard BB1 is simply expressed by "BB1 AND NOT BB2". The "NOT BB2" part ensures that the BB2 bits are cleared when AND-ing with it. Doing "(BB1 AND BB2) XOR BB1" is possible as well but why would you prefer obfuscation over clarity?
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 1:49 pm
Luis Babboni wrote: Fri Mar 26, 2021 1:12 pm
Sven wrote: Fri Mar 26, 2021 1:07 pm That is not my point.

The goal is to clear all bits on file A.

I do this by clearing all bits on file A. (... AND NOT FileA)

You do this by clearing all other pawn bits and then flipping all pawn bits again. That is cryptography 😎
So is a matter of "sthetic"? (Not few thinking in a future understanding)
No, a matter of "straight forward thinking". Just don't wrap your head around how fast those bit operations are (and the difference is not that big). You are currently in a phase where you want to try to understand basic bitboard concepts. The basic concept of clearing some bits BB2 in a bitboard BB1 is simply expressed by "BB1 AND NOT BB2". The "NOT BB2" part ensures that the BB2 bits are cleared when AND-ing with it. Doing "(BB1 AND BB2) XOR BB1" is possible as well but why would you prefer obfuscation over clarity?
I choiced the first just cause was the first I could find, no more.

Thanks for the hints!
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 1:07 pm Mmmmm, may be the NOT could be faster, use just one BB, but no matter that, I could have a NOTFilaA BB stored since the engine boot. :wink:
I´m right?
Thanks!
On the other hand, having a precomputed table would introduce a memory load which is not fast at all.

And some processors might even have AND NOT as a single instruction.

https://www.felixcloutier.com/x86/andn

Perhaps this is the last % to focus on when the engine already beats 99% of the other engines and there is nothing else to improve? :?
User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: Question about the way to use bitboards.

Post by mvanthoor »

Maybe it's easier if there is some example.

Code: Select all

bb_a: 1011 0110
bb_b: 1100 0100
You want to clear the bits in bb_a, that are in bb_b. So what you want is, very literally:

you want the bits in bb_a AND NOT the ones in bb_b

Code: Select all

bb_a:		 1011 0110
AND
NOT bb_a:	 0011 1011  (not bb_b is the reverse of bb_b above)
============================
result		 0011 0010
As you can see, the that were set in bb_b, are now removed from bb_a. (If the bit that was set in bb_b was not set in bb_a, it was already not set, so it stays not set.)

Bit operations are so fast you don't need to care about speed. Almost anything else you do (loops, ifs, extensive evaluation terms) will slow the engine down more than doing an unnecessary bit operation here or there.
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL
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 »

mvanthoor wrote: Fri Mar 26, 2021 10:09 pm ...
Bit operations are so fast you don't need to care about speed. Almost anything else you do (loops, ifs, extensive evaluation terms) will slow the engine down more than doing an unnecessary bit operation here or there.
Nice! Thanks!