Bitboard question (from xiphos code)

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
User avatar
xr_a_y
Posts: 535
Joined: Sat Nov 25, 2017 1:28 pm
Location: France

Bitboard question (from xiphos code)

Post by xr_a_y » Mon Dec 31, 2018 1:13 pm

In xiphos SEE, one can read

Code: Select all

  else if (sq == pos->ep_sq && p == PAWN)
  {
    occ ^= (1ULL << (sq ^ 8));
    gain[0] = piece_value[PAWN];
  }
about the case where the destination square (sq) of the initial move is the ep move.

Can someone explain this

Code: Select all

     occ ^= (1ULL << (sq ^ 8)); 
, I don't get it. :oops:

Thanks

User avatar
hgm
Posts: 23211
Joined: Fri Mar 10, 2006 9:06 am
Location: Amsterdam
Full name: H G Muller
Contact:

Re: Bitboard question (from xiphos code)

Post by hgm » Mon Dec 31, 2018 2:17 pm

I have never looked at any Xiphos code, but it seems to me (sq ^ 8) is the square that contains the victim Pawn if sq is the to-square. The line removes the Pawn from the 'occupied' bitboard.

User avatar
xr_a_y
Posts: 535
Joined: Sat Nov 25, 2017 1:28 pm
Location: France

Re: Bitboard question (from xiphos code)

Post by xr_a_y » Mon Dec 31, 2018 3:11 pm

That was of course what I suspected but now I understand, this is toggling the fourth bit ... so between 24 (b01000) and 31(b11111) this removes the 1 in the fourth bit so it gives the number minus 8 and between 32 (b100000) and 39 (b100111) this add a 1 in the fourth bit so it gives the number plus 8.
Tricky ... Bitboards are amazing ...

Sven
Posts: 3770
Joined: Thu May 15, 2008 7:57 pm
Location: Berlin, Germany
Full name: Sven Schüle
Contact:

Re: Bitboard question (from xiphos code)

Post by Sven » Mon Dec 31, 2018 4:34 pm

xr_a_y wrote:
Mon Dec 31, 2018 3:11 pm
That was of course what I suspected but now I understand, this is toggling the fourth bit ... so between 24 (b01000) and 31(b11111) this removes the 1 in the fourth bit so it gives the number minus 8 and between 32 (b100000) and 39 (b100111) this add a 1 in the fourth bit so it gives the number plus 8.
Tricky ... Bitboards are amazing ...
For me this is not so much about bitboards but about 6-bit square numbering (although you may state that this numbering system is most common in bitboard engines ...). In a system where squares are numbered from 0 to 63 rank by rank (so that bits 0-2 contain the file part and bits 3-5 the rank part) the operation "sq ^ 8" toggles the rank betwen 0 and 1, 2 and 3, 4 and 5, or 6 and 7, which can be useful in some areas, one of them being the manipulation of square numbers related to en passant.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)

Look
Posts: 148
Joined: Thu Jun 05, 2014 12:14 pm
Location: Iran
Full name: Mehdi Amini
Contact:

Re: Bitboard question (from xiphos code)

Post by Look » Mon Dec 31, 2018 4:50 pm

xr_a_y wrote:
Mon Dec 31, 2018 1:13 pm
In xiphos SEE, one can read

Code: Select all

  else if (sq == pos->ep_sq && p == PAWN)
  {
    occ ^= (1ULL << (sq ^ 8));
    gain[0] = piece_value[PAWN];
  }
about the case where the destination square (sq) of the initial move is the ep move.

Can someone explain this

Code: Select all

     occ ^= (1ULL << (sq ^ 8)); 
, I don't get it. :oops:

Thanks
The line better has been a named function, so you can comprehend what it is doing.
Mehdi Amini
www.my-c-codes.com/

Farewell.

D Sceviour
Posts: 417
Joined: Mon Jul 20, 2015 3:06 pm
Contact:

Re: Bitboard question (from xiphos code)

Post by D Sceviour » Mon Dec 31, 2018 4:51 pm

This a very fast way of finding the en passant square for either move color. I have never seen the use of XOR (sq ^ 8) in any other code before. Is Xiphos the first to use this?

Ratosh
Posts: 61
Joined: Mon Apr 16, 2018 4:56 pm

Re: Bitboard question (from xiphos code)

Post by Ratosh » Mon Dec 31, 2018 4:53 pm

A few square tricks:
  • Invert the square rank: (square xor 56)
  • Relative square: (square xor color * 56)
  • Invert rank: (rank xor 7)
  • Relative rank: (rank xor color * 7)
  • Square file: (square and 7)
  • Square rank: (rank >> 3)
  • Mirror file: (file xor 7)

User avatar
Kotlov
Posts: 197
Joined: Fri Jul 10, 2015 7:23 pm
Location: Russia

Re: Bitboard question (from xiphos code)

Post by Kotlov » Mon Dec 31, 2018 6:02 pm

D Sceviour wrote:
Mon Dec 31, 2018 4:51 pm
This a very fast way of finding the en passant square for either move color. I have never seen the use of XOR (sq ^ 8) in any other code before. Is Xiphos the first to use this?
No, is ordinary.
Eugene Kotlov
Hedgehog 2.0 64-bit coming soon...

Sven
Posts: 3770
Joined: Thu May 15, 2008 7:57 pm
Location: Berlin, Germany
Full name: Sven Schüle
Contact:

Re: Bitboard question (from xiphos code)

Post by Sven » Mon Dec 31, 2018 6:03 pm

D Sceviour wrote:
Mon Dec 31, 2018 4:51 pm
This a very fast way of finding the en passant square for either move color. I have never seen the use of XOR (sq ^ 8) in any other code before. Is Xiphos the first to use this?
I also found (sq ^ 8) at one place in Rodent 1.6. Since I do not have many open-source chess engine versions on my harddisk I guess this is sort of a random hit and probably many others have already used that trick.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)

User avatar
hgm
Posts: 23211
Joined: Fri Mar 10, 2006 9:06 am
Location: Amsterdam
Full name: H G Muller
Contact:

Re: Bitboard question (from xiphos code)

Post by hgm » Tue Jan 01, 2019 8:30 am

D Sceviour wrote:
Mon Dec 31, 2018 4:51 pm
This a very fast way of finding the en passant square for either move color. I have never seen the use of XOR (sq ^ 8) in any other code before. Is Xiphos the first to use this?
Micro-Max (which is a mailbox engine) has used it from the beginning (i.e. the first version that implemented e.p.):

Code: Select all

      if(p<3&y==E)H=y^16;                      /* shift capt.sqr. H if e.p.*/
Which, in the more verbose 'maximax.txt' version, reads as:

Code: Select all

            if(PieceType<3 & ToSqr==epSqr)
              CaptSqr = ToSqr^16;                      /* shift CaptSqr if e.p.    */
PieceType < 3 test for Pawns (which have PieceType 1 and 2). Of course it uses 16 here rather than 8 because it uses a 0x88 numbering scheme for the squares.

It uses a similar trick in getting the two squares next to the Rook for testing castling pseudo-legality: sqr^1 and sqr^2, independent of which corner the Rook is in. Unfortunately this only works on the king side for boards with a width that is a multiple of 4, which was a problem in Fairy-Max.

Post Reply