We've all gotten so used to high-level programming that we often forget about how compact and clean machine code really is. Wait a minute, let's not go there. Machine code may be efficient but who wants to go back to using 1's and 0's in their rapid application development?

We usually can't use rapid development and efficient code in the same sentence, but there are some areas where the two play nicely together, namely in data storage.

Consider that you have a database table which stores your customer's preferences. One such column is used to store the days of the week that they prefer to be emailed about daily specials.

You could create a cross-reference table to store the days of the week, you could create a simple varchar(8) field to store the day numbers or letters. Or, you could revisit those 1's and 0's again.

What if each day of the week was represented by one bit in an 8-bit TINYINT field? Or, if you have a newer version of MySQL, the bit data type would be more appropriate.

1001001 would mean: Sunday, Wednesday, Saturday

In base 10, this number is 73, and fits nicely inside a single-byte TINYINT field, as suggested above.

Just about every coding language out there provides functions to convert between Base 2 and Base 10. So, consider this approach when you have a large number of records, and storage space is at a premium.

See the Determine if a bit is on or off coda post if you are not using the MySQL bit field.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *