Huy's Notes
Packing two bytes into an integer

Packing two bytes into an integer

#hardware #keyboard

I'm working on a 40% keyboard which has a 12x4 layout, and the firmware for now just need to support one key at a time.

The key scanning function returning a single integer, so I need to pack the two values Column and Row of a pressed key into this integer.

Since both of the column and row values are quite small (less than 255), I can just use two bytes to represent them. Therefore, I can pack them into an integer easily by using bit shift operators. Put the row value to the 8 low bits, and the rest is for the column value.

RESULT: int = (COL << 0) | (ROW << 8)

Later on in key sending logic, I can extract them all by shifting it back again:

COL: byte = (RESULT >> 0)
ROW: byte = (RESULT >> 8)

Referred in

If you think this note resonated, be it positive or negative, please feel free to send me an email and we can talk.