I think it like this:
Question about the way to use bitboards.
Moderators: hgm, Rebel, chrisw
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina
Re: Question about the way to use bitboards.
Last edited by Luis Babboni on Fri Mar 26, 2021 1:02 pm, edited 1 time in total.
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina
Re: Question about the way to use bitboards.
I do not understand enough of how the operators and bits works to see why is like this, sorry.Sven wrote: ↑Fri Mar 26, 2021 12:53 pmYou 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?
The NOT is faster than all others?
-
- 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.
That is not my point.Luis Babboni wrote: ↑Fri Mar 26, 2021 1:02 pmI do not understand enough of how the operators and bits works to see why is like this, sorry.Sven wrote: ↑Fri Mar 26, 2021 12:53 pmYou 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?
The NOT is faster than all others?
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)
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina
Re: Question about the way to use bitboards.
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.
I´m right?
Thanks!
I´m right?
Thanks!
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina
Re: Question about the way to use bitboards.
So is a matter of "sthetic"? (Not few thinking in a future understanding)
-
- 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.
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?Luis Babboni wrote: ↑Fri Mar 26, 2021 1:12 pmSo is a matter of "sthetic"? (Not few thinking in a future understanding)
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.
I choiced the first just cause was the first I could find, no more.Sven wrote: ↑Fri Mar 26, 2021 1:49 pmNo, 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?Luis Babboni wrote: ↑Fri Mar 26, 2021 1:12 pmSo is a matter of "sthetic"? (Not few thinking in a future understanding)
Thanks for the hints!
-
- 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.
On the other hand, having a precomputed table would introduce a memory load which is not fast at all.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.
I´m right?
Thanks!
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?
-
- Posts: 1784
- Joined: Wed Jul 03, 2019 4:42 pm
- Location: Netherlands
- Full name: Marcel Vanthoor
Re: Question about the way to use bitboards.
Maybe it's easier if there is some example.
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
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.
Code: Select all
bb_a: 1011 0110
bb_b: 1100 0100
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
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.
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina