Answers.Body: <p>In the beginning, they thought that using a right shift (or a left shift), but they found that, as with other systems, bit would fall off the board.  To correct this, they computed every move from each square for each piece.  This takes a performance hit only when the page loads.  The other alternative they tried was to just load precomputed bitboards which increases the data slightly.</p>
  
  <p>I've developed a system which has drawbacks including being slightly slower, but it's easier to understand.</p>
  
  <p>First you need three arrays:</p>
  
  <pre><code>const int movement[64] ={
  
  17171, 50075, 50075, 50075, 50075, 50075, 33179, 32905, 
  
  18231, 51199, 59391, 59391, 59391, 59391, 41471, 41165, 
  
  20279, 57343, 65535, 65535, 65535, 65535, 45567, 45261, 
  
  20279, 57343, 65535, 65535, 65535, 65535, 45567, 45261, 
  
  20279, 57343, 65535, 65535, 65535, 65535, 45567, 45261, 
  
  20279, 57343, 65535, 65535, 65535, 65535, 45567, 45261, 
  
  20023, 24319, 32511, 32511, 32511, 32511, 14591, 12493,
  
   3110, 23662, 15470, 15470, 15470, 15470, 14446, 12364
  
  };
  
  const int dir[16] ={
      8, 1, -8, -1, 9, -7, -9, 7, 17, 10, -6, -15, -17, -10, 6, 15
  };
  
  const int key[16] = {
      1, 2, 4, 8, 16, 32, 64, 128,
      256, 512, 1024, 2048, 4096, 8192, 16384, 32768
  };
  </code></pre>
  
  <p>This is constructed so that if the first bit (<code>key[0]</code>) is set, then the relative movement of <code>+8</code> (<code>dir[0]</code>) is possible;  (Showing this in hex would display the relative values better.)</p>
  
  <p>The move generation is six functions each containing about 9 lines each.  The bitboard generation is shorter and faster, but harder to implement.  Using <a href="https://www.chessprogramming.org/Magic_Bitboards" rel="nofollow noreferrer">magic bitboards</a> is even faster, but it's even harder to understand.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-01T07:37:09.307Z
Answers.Text: https://github.com/kbjorklu/chess/blob/master/src/bitboard.js gives a complete JS chess engine.
Answers.UserDisplayName: Mike Jones
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-30T13:22:37.023Z
Answers.LastActivityDate: 2020-01-30T13:32:11.767Z
Answers.LastEditDate: 2020-01-30T13:32:11.767Z
Answers.UserDisplayName: Mike Jones
Body: <p>To make my engine more efficient, I am writing my engine such that it uses bit-boards in its move generation. But I have a question, suppose I have a rook move mask centered at a specific spot, how might I transpose the rook mask so that it shows the moves for a different position? eg:</p>
  
  <pre><code>from this mask:
  [
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [1, 1, 1, "R", 1, 1, 1, 1],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0]
  ];
  
  to this mask:
  [
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [1, 1, 1, "R", 1, 1, 1, 1],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0]
  ];
  
  </code></pre>
  
  <p>If you need any clarifications regarding the question feel free to ask.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-30T03:13:42.073Z
Comments.Text: That would depend what language you are using wouldnt it? In APL I would just rotate the matrix up circular one unit.  Or maybe that is down, I have not used APL for a long time now.
Comments.UserDisplayName: edwina oliver
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-30T03:21:32.597Z
Comments.Text: @edwinaoliver I am using Javascript if that helps.
Comments.UserDisplayName: Crupeng
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-30T03:29:10.050Z
Comments.Text: Thanks. That will help whoever answers.    I do not use javascript.
Comments.UserDisplayName: edwina oliver
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-30T12:58:30.403Z
Comments.Text: The programming language is in the title.
Comments.UserDisplayName: Mike Jones
Tags: <engines><programming>
Title: Chess move generation with bit boards in Javascript
ThreadText: # Chess move generation with bit boards in Javascript
  
  (Asked by: username_0 on 2020-01-29)
  
  To make my engine more efficient, I am writing my engine such that it uses bit-boards in its move generation. But I have a question, suppose I have a rook move mask centered at a specific spot, how might I transpose the rook mask so that it shows the moves for a different position? eg:
  
  ```
  from this mask:
  [
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [1, 1, 1, "R", 1, 1, 1, 1],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0]
  ];
  
  to this mask:
  [
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [1, 1, 1, "R", 1, 1, 1, 1],
      [0, 0, 0, 1, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 0, 0, 0]
  ];
  
  ```
  
  If you need any clarifications regarding the question feel free to ask.
  
  **Tags:** engines, programming
  
  > **Comments:**
  > **username_1**: That would depend what language you are using wouldnt it? In APL I would just rotate the matrix up circular one unit.  Or maybe that is down, I have not used APL for a long time now.  
  > **username_0**: @username_1 I am using Javascript if that helps.  
  > **username_1**: Thanks. That will help whoever answers.    I do not use javascript.  
  > **username_2**: The programming language is in the title.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2020-01-30. Score: 4 | Accepted answer
  
  In the beginning, they thought that using a right shift (or a left shift), but they found that, as with other systems, bit would fall off the board. To correct this, they computed every move from each square for each piece. This takes a performance hit only when the page loads. The other alternative they tried was to just load precomputed bitboards which increases the data slightly.
  
  I've developed a system which has drawbacks including being slightly slower, but it's easier to understand.
  
  First you need three arrays:
  
  ```
  const int movement[64] ={
  
  17171, 50075, 50075, 50075, 50075, 50075, 33179, 32905, 
  
  18231, 51199, 59391, 59391, 59391, 59391, 41471, 41165, 
  
  20279, 57343, 65535, 65535, 65535, 65535, 45567, 45261, 
  
  20279, 57343, 65535, 65535, 65535, 65535, 45567, 45261, 
  
  20279, 57343, 65535, 65535, 65535, 65535, 45567, 45261, 
  
  20279, 57343, 65535, 65535, 65535, 65535, 45567, 45261, 
  
  20023, 24319, 32511, 32511, 32511, 32511, 14591, 12493,
  
   3110, 23662, 15470, 15470, 15470, 15470, 14446, 12364
  
  };
  
  const int dir[16] ={
      8, 1, -8, -1, 9, -7, -9, 7, 17, 10, -6, -15, -17, -10, 6, 15
  };
  
  const int key[16] = {
      1, 2, 4, 8, 16, 32, 64, 128,
      256, 512, 1024, 2048, 4096, 8192, 16384, 32768
  };
  
  ```
  
  This is constructed so that if the first bit (`key[0]`) is set, then the relative movement of `+8` (`dir[0]`) is possible; (Showing this in hex would display the relative values better.)
  
  The move generation is six functions each containing about 9 lines each. The bitboard generation is shorter and faster, but harder to implement. Using [magic bitboards](https://www.chessprogramming.org/Magic_Bitboards) is even faster, but it's even harder to understand.
  
  > **Comments:**
  > **username_2**: https://github.com/kbjorklu/chess/blob/master/src/bitboard.js gives a complete JS chess engine.  
  
  ---
  
  