There are 10 types of people in this world…

Ah, the age old nerd adage: “There are 10 (correctly pronounced ‘two’) types of people in this world, those who understand binary and those who do not.”  So, besides knowing that  10  is the binary representation of the decimal number  2 , how well do you know binary?  And do you put this knowledge to use in your every day programming?

Your computer’s RAM is measured in bytes.  Multiply this number by eight and that’s how many bits your computer has to represent a zero or one. It’s this set of billions of on/off switches that your computer uses to store all of the information it needs to run all your programs.

Now, unless you’re writing code for an embedded system where available RAM might actually be limited, these days we have such an abundance of RAM that wasting a handful of bits here and there and everywhere usually isn’t that big of a deal.  But it’s important to understand that your processor is accessing memory locations by the byte, and not by the individual bit.  You only need an invidivual bit to represent a true/false value–it’s either on or off.  But if you create a  bool  variable, or an array of  bool s, you’re using an entire byte each.  For each variable, 7 bits are effectively wasted.  Out of a possible 256 values a byte can represent, a  bool  only needs two.

But if we ever have a need to represent multiple on/off type values, we can unlock these extra 7 bits using a bit of bit-masking.  Consider the following   enum :

Now we can use a single variable (and a single byte) to express what would ordinarily take four variables (and four bytes) to represent.  Perhaps more importantly, consider that processors access and handle memory and instructions by the byte–not by the bit.  If we use four  bool  variables, it takes up to four CPU cycles to determine if our two font-styles are the same, but using a bit mask with this  FontStyle  type, the CPU can compare it all in a single cycle.

To demonstrate the difference, let’s first show what a  struct  for a “FormattedString” might look like without using this bit-masking.

Initialization seems kind of clunky, and comparing the exact formatting of each is definitely clunky.  Let’s take a look at implementing the same logic using bit-masking to see how much clear we could make it:

Our initialization is more clear.  Instead of just sending four on/off values, we’re sending a single value which carries a particular alignment of bits to demonstrate which values are turned on and off.  Our previously declared  enum is there to make it explicit which bits mean what.  And our comparison is more clear (and technically, faster).  We’re not comparing whether each is bold/underline/italic/strikeout.  We’re comparing whether they have the same font style. Our bitmask version is faster because we’re comparing just one byte rather than four bytes.

Moreover, using this bitmask, we can always go add more fields, and everything we’ve currently implemented will still work just as it does.  If we add more format options to our struct, all of our already long conditionals to determine if our strings have the same formatting options will have to have yet another condition added or risk being wrong, but the  s1.fontStyle == s2.fontStyle  will automatically start comparing the 5th option.  Moreover, this approach makes it easier to copy font style from one to another, as we simply need to do  s1.fontStyle = s2.fontStyle  rather than copying each individual flag one at a time.

Using bit-masking can go a long way toward cleaning up your code, but when doing this, it’s important that the bits all have a clearly defined meaning, and using an  enum  is the best way to do that. But as a final note, I want to be clear. We shouldn’t be bitmasking purely because it’s more efficient. Bitmasking is more efficient, yes, but on such a miniscule level that if we’re bitmasking for the sake of optimization, it’s most likely that we’ve fallen prey to premature optimization. We should be using bitmasking because it makes our code make more sense.

Leave a Reply