I am a hobby programmer doing some things with Poker abstraction. I have cards encoded in 8 bits, the least significant half encodes the rank. the next two bits its suit-id (not suit per se, since then I would needlessly add complexity, It starts with 00 at the first card and only gets incremented when a new card doesn't match a card that is already in play). At the most significant 2 bits I encoded the card location 00 for Hand, 01 for flop and so on.
In order to extract the data I am unsure what is better, bit-masking using predefined masks? or bit-shifting operations? (or something completely different?). Below is an example written in golang of a function I wrote and rewrote to extract the rank of the card. Is one superior to the other?
Example 1:
func GetRank(card byte) byte { // extrapolates rank from card
var rankmask byte
rankmask= 240 // represents 11110000 in binary
return card &^ rankmask // knocks down the location and suit info
}
VS example 2:
func GetRank(card byte) byte { // extrapolates rank from card
bc <<= 4 //shift away location and suit-id
return bc >> 4 //shift rank back into place
}
I read that bit-shifting is supposed to generally be faster, but I would be doing 2 longer bit-shift operations in comparison to one bit-masking.