FunStuff‎ > ‎

What's a BitOp?

Bitops are an really interesting way to reduce a large selection of bits (or yes/no answers) into a single number.  Bit ops use numbers that only allow for one possible way to arrive at the end value.  Let me explain that in a different way.

If you need to give someone $0.25 in change, you could do it in more than one way.  You could give them 1 quarter, or 2 dimes and a nickel, or 25 pennies.  There are many ways to get to $.025

With a bit op, this cant happen.  a bitop uses numbers starting with: 1,2,4,8,16,32,64,128,256  (See a pattern here?).  If you look at the numbers, you will see there is only one way to make 8. same with all the other numbers listed.  Because of this, a number can hold many unique combinations of bits.  

Let's try a few examples.  Lets say we have a checklist with 7 check boxes.  Below each checkbox is a value I have assigned it:

 X    X 
 216 32 64 

I can describe the set of checkboxes with a 2 digit number.  41.   

Another example:

  X  X  X
 1 2 816 32  64

The total is 86.  Try to make 86 any other way using each number only once.  (Hint: You can't!)

This same technique is used to save space in a database.  If I had a form with 9 checkboxes, normally I would have 9 fields in the database with 1 digit each (or technically a bit).  Instead, I can have 1 field with 3 digits.

This technique pops up quite often in electronics, and computer networking.  Learn it now. I'm sure you'll see it again!


Comments