# Understanding magic bitboard

I have read a lot of stuff about magic bitboard but some stuff is not clear. A lot of authors say to create the move masks for the piece avoiding the border positions. So, for a bishop at `D3` the move mask would be this:

``````00000000
00000000
00000010
01000100
00101000
00000000
00101000
00000000
``````

``````00000000
00000001
00000010
01000100
00101000
00000000
00101000
01000100
``````

They say this is good for convenience. I don't see how, but anyway, lets bear with this idea.

The problem is that, if we will avoid the border are, how will the move map for a hook at `A1` be? All zeros? The hook at `A1` will be like this:

``````10000000
10000000
10000000
10000000
10000000
10000000
10000000
11111111
``````

but if we cannot have the numbers at the border it will be all zeros.

Is there another reason for that "avoid the border" rule? Is that OK if I forget that rule and make it to the border?

It's not about avoiding the border. It's about avoiding the last bit of the ray.

Given this:

``````10000000
10000000
10000000
10000000
10000000
10000000
10000000
01111111
``````

(Note that A1 should be zero as the rook is unimportant)

You will note that along the first rank, if there is no piece on G1, then a rook can attack H1, regardless of what is on that square. This also applies along the A-file, where if there is no piece on A7, then A8 can always be attacked.

Because of this, we mask out the last bit of the ray, as we do not need to know what's at the end of the ray, producing this mask:

``````00000000
10000000
10000000
10000000
10000000
10000000
10000000
01111110
``````

This produces a significant table reduction, as you need only 12 bits of occupancy to index the table, not 14.

• ok thanks for the explanation but what I do not understand is this. Suppose there is a piece at H1 and I am trying to discover if the hook is attacking that. If that H1 bit is zero how do I will ever discover that?
– Duck
Aug 2, 2016 at 4:08
• Because the attack table that the magics index will have the bit set as needed. Remember that the mask and the attack table are different things. Aug 2, 2016 at 14:42
• Perhaps you should try to use something other than magics for your program. Perhaps you could try Obstruction Difference instead? Aug 2, 2016 at 14:47
• I don't know. I have read a lot of stuff but nobody explains what is really needed to make this work. Many people try but their explanations derail after the the third paragraph. From that point on, it is impossible to understand what they are talking about. Magic index for example: they say is an index that converts one stuff into another... example that one can understand? nope.
– Duck
Aug 2, 2016 at 15:50
• It's called a magic number for a reason. Have you read the CPW article on it? That tends to help. Also have a look at kindergarten bitboards, which were what magic bitboards were derived from. Aug 2, 2016 at 18:34