I am currently helping teach a Computer Programming class. (I am in my Senior Year in High School, TA-ing for my Computer Programming teacher.) Right now the students are learning about bit-wise operators, but some more advanced ones are asking me about using bits and bit-wise operations to store data in the bits. So far what we they is pretty simple, but effective giving 50% (give or take 5%) compression rate. We take our playing card, let's say an Eight of Diamonds, and turn it into a 7 bit value 0b1011000. This works for all generic playing cards Ace - King, and all suits, and you can just put the bit (or integer) into a function and you will get a playing card back.
Here is what they have come up with so far.
0b1 SS VVVV
0b1 keeps all the 'playing card turned bits' at a consistent length.
SS is the suit value
VVVV is the card value
Here is how they form their bits
suits = ['Hearts','Diamonds','Spades','Clubs']
ranks = ['Ace','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King']
For this lets use the Jack of Diamonds
Step 1. Isolate the Suit and convert into bis. The 'suit bits' are equal to the index number of the suit in binary so Diamonds would be 0b01 or 0b1
Step 2. Shift your suit bits left 4 bits. 0b01 -> 0b010000
Step 3. Isolate the Rank and convert into bits. The 'rank bits' are equal to the index of the number of the suit in binary so Jack (11) would be 0b1011
Step 4. Add your rank bits into your suit bits. 0b010000 + 0b1011 = 0b011011
Step 5. Add your combined rank\suit bits to 0b10000000 to give a consistent length across all values. 0b011011 + 0b1000000 = 0b1011011
In a full deck of cards (All 52 cards no jokers) there are 741 characters in the card names. A full deck of cards converted to bits is stored in 364 bits with 49% compression while still keeping 100% of the data.
I'm wondering if there's any more efficient way to store this data. I'm only in my 2nd year of Computer Programming, and I only have highschool level education, so I wouldn't know very much about this myself.
Here is their code http://repl.it/BA56