I'm following this: Optimizing hand-evaluation algorithm for Poker-Monte-Carlo-Simulation
My setup is similar -- an unsigned long to represent the board and the player's holding. All cards are bit flags, starting at 1 and ending at 2^51.
For example, 2c is 1, 3c is 16, 4c is 256, and As is 2^51. So, the hand
1010 0000 1001 0000 0000 0100 0000 0001 0000 0000 0010 is
Qs Qd 7h 5c 2d (the board) + Ts Tc (the player's holding).
The link I referred to explains well how to find a straight flush / four of a kind, and it works. However, I seem to have hit a brick wall trying to identify three of a kind. I've tried:
hand & (hand >> 1) & (hand >> 2) & 0x1111111111111111 -- the naive case, following the example from the link;
hand & (hand >> 2) & 0x3333333333333333 -- this seems to catch all three of a kind, but it misclassifies pairs (e.g. the example above) as trips;
How do I dismiss all nibbles with fewer than three set bits?
This problem is closely related to calculating the Hamming weight. That is you are insterested in the number of set bits for each nibble. The steps to start with are:
int64 w = hand - ((hand >> 1) & 0x55555555);
w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
This results in the number of set bits of each nibble in w. You'd then check each nibble of w to contain 3. Like so: if ((w & 0xf) == 0x3 || (w & 0xf0) == 0x30 ...
.