OK, let's consider a 64-bit number, with its bits forming a 8x8 table.
E.g.
0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 0 1 1 0 1 0 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 0 1 1 0 1 0 1 0
written as
a b c d e f g h
----------------
0 1 1 0 1 0 1 0
0 1 1 0 1 0 1 1
0 1 1 1 1 0 1 0
0 1 1 0 1 0 1 0
1 1 1 0 1 0 1 0
0 1 1 0 1 0 1 0
0 1 1 0 1 1 1 0
0 1 1 0 1 0 1 0
Now, what if we want to isolate JUST e.g. column d (00100000
) (or any row/diagonal for that matter) ?
Can this be done? And if so, how?
HINTS :
(a) My main objective here - though not initially mentioned - is raw speed. I'm searching for the fastest algorithm around, since the "retrieval" function is being performed some millions of times per second.
(b) This is what comes closer to what I mean : https://www.chessprogramming.org/Kindergarten_Bitboards
Perhaps a somewhat more optimized idea:
If b is a constant, this will perform slightly better.
Another way may be:
Doing one "and" rather than many helps speed it up.
This one is from the Chess Programming Wiki. It transposes the board, after which isolating a single row is trivial. It also lets you go back the other way.
You can transpose the number, and then simply select the relevant column, which is now a row, and therefore contiguous bits, as you wanted.
In my tests it wasn't much better than ORing together 8 individually selected bits, but it is much better if you intend to select multiple columns (since the transpose is the limiting factor).