A Single Number Can Be Code
April 6, 2025
If you keep a tin of shoe cream in the sock drawer, then you can understand why, sometimes, you might want to cram four different values into a single number.
It makes a kind of sense. The time you are most likely to think, “Hmm, these shoes could use a dab of shoe cream,” would be when you are reaching for clean socks. It's not crazy to keep the product with the socks as long as it does not actually ooze out into the socks.
In the same way, a digital clock might store two, different values as a single number stored in one memory cell. Suppose the value of the current hour can range between 1 and 12. This value can be combined with a second number, 0 or 1, indicating whether the hour is AM or PM.
My current software-writing project involves a digital clock that actually stores four, different values related to the hour, all encoded together as a single number.
In addition to the two mentioned above, there are: a number, 0 or 1, indicating whether the hour is in the military-style range of 0-23; and a number, again 0 or 1, indicating whether an alarm should go off when the real-time hour value changes to match a certain, pre-selected value stored elsewhere.
I wrote one set of software instructions designed to take those four, different quantities and pack them into a single number. Of course the project also needs instructions for reversing the process.
Here, your Dear Diarist pauses amidst temptation to go into details. As the jingle sings, Oh! what fun it is to write in a one-horse open sleigh! He sighs, silently, gazing for a moment into the middle distance...
Tell you what: I will hold that thought until the end.
The “smart” doorbells, thermostats and appliances connecting modern homes to the internet of things probably use this technique a lot. They gather data and send it away to be stored elsewhere. Why transmit four numbers separately when compressing them into a single value can make the whole internet run faster?
The software instructions to pack and to unpack compressed data follow a best-practice for coders: write-once-use-many-times.
It is worth mentioning another best-practice here: documentation. As in, I really should stick a post-it note on that sock drawer: “The shoe cream is in here!”
Technical Bit About Bits
You might want to skip the rest of this...
I will probably hate myself for writing what follows here. I might even delete it pretty soon. But I am a compulsive communicator so here goes, if only to get this bee of explanation out of my head and make it stop buzzing around in there.
The most important two numbers for understanding programmable electronic devices are ‘1’ and ‘0’. They can be stored in a single bit of memory. At the physical level, a bit is a kind of microscopically small electrical switch that can be turned on and off.
Another important number is 255, the maximum value that can be represented by lining up eight bits together into a single group and switching all of them ‘on’. The devices I usually write programs for store information in such 8-bit groups, called bytes.
It needs only four or five of the bits in a byte to represent the value of an hour on a clock, such as 12. Suppose we confine those five bits to one end of the line. Call them bits number 1 through 5. This leaves three more bits available to use some other way, in the same byte but at the other end of the line.
We have programming instructions for accessing individual bits within a byte. We can turn a certain bit on or off. Also, we can ‘read’ whether a bit is presently on or off. Suppose we decide to use bit number 6 to indicate whether the hour is AM (bit 6 = 0) or PM (bit 6 = 1). We can read that bit to find out. And we can change it. Meanwhile, changing bits 1 through 5 to update the hour would have no effect on bit 6.
An 8-bit byte can be interpreted as a single number in the range of 0 to 255. However, it can also be interpreted as I have done here: a collection of different bits having distinct meanings independent of each other. As long as I know what each bit represents, I can store the group of them together in a single byte. Technical documents called datasheets tell us programmers which bits go where.
Love me for it, or loathe me, but I am a Geek and seldom feel happier than when whiling away a couple of days working out ways to wrangle a herd of numbers into the bits of a single byte.