The mystery of 9 bits contained within 8 bits

A project log for Making music with relay chatter

One of the Hackaday 2018 entries is to make a musical instrument of sorts and I thought of the sounder used by Morse Code operators

boelens-lelandBoelens, Leland 09/30/2018 at 07:550 Comments

I found a way of storing 9 bits of information within 8 bits.

Morse code has 1 dit or 1 dah up to a combination totaling 6 bits of information.

I decided that I would represent a 0 as a dit, and a 1 as a dah. Then I decided I would use the lower 3 bits to represent a bit count register. Bits 1 thru 5 were possible and still fit into 8 bits. It took more thought to work out a work around to get 6 bits of Morse  code and have a 3 bit count register.  Then it occurred to me that I could use the binary 001, 010, 011, 101 as is, I just needed to work in 110 and 100. I recognized that 000 and 100 had both lower bits as a 0 and this was not the case with 1,2,3, and 5.  I also realized that the binary "4" bit position was the needed 6 th bit. So I came up with a numbering and a conversion scheme that works. B000 and B100 represent that 6 bits of Morse code are in this byte. B111 represents that 4 bits of Morse code are contained in this byte. B110 represents this byte contains no valid Morse code, skip this byte.

The Morse code byte is unpacked in this manner. The 3 LSB bits are masked out and tested. First for a B110, to skip around outputting bad data. Then if B000 or  B100 is present then the bit count is set to B110 and the Morse code byte is right shifted 2 times to position the LSB into bit D0 maintaining the 6 Morse code bits or shifted right 3 times to position the 1 thru 5 bits of Morse code to align the LSB with D0. The final step is to test if the bit count register contains B111,  then if B111 is present the bit count is set to B100.

So in the end I was able to pack 6 bits of Morse code plus a 3 bit count register totaling 9 bits, into 8 bits of data.