Do You Speak Binary?

Tuesday, October 20, 2009

How well do you know the fundamental unit of information, communication, computation?

When interviewing coders, I often like to ask them to explain how they would implement a function which counts the number of bits set in an integer. This is often called the “population counting” problem. In Beautiful Code: Leading Programmers Explain How They Think, Henry S. Warren, Jr. gives a wonderful treatment of the population count problem. This C code implements a clever algorithm to compute number of set bits in one 32-bit word:

/* Counts 1-bits in a word. */
int pop(unsigned x) {
  x = x - ((x >> 1) & 0x55555555);
  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  x = (x + (x >> 4)) & 0x0F0F0F0F;
  x = x + (x >> 8);
  x = x + (x >> 16);
  return x & 0x0000003F;
}

Can you figure out how it works?

For a really hard-core puzzle, try to figure out this piece of code from the Quake code. It computes the inverse square root of a floating point number:

float InvSqrt(float x){
   float xhalf = 0.5f * x;
   int i = *(int*)&x;
   i = 0x5f3759d5 - (i >> 1);
   x = *(float*)&i;
   x = x*(1.5f - xhalf*x*x);
   return x;
}

Wow. If you need to cheat (I did), the web is full of explanations. The one that really helped me understand was by Kalid Azad at BetterExplained.

I firmly believe that every programmer should try to build a good working understanding of the bit world. It’s not that you have to know that stuff to be able to write (most) programs. But look at it this way. That computer on your desk has a CPU in it. That CPU is executing billions of instructions per second to execute your code. Are you really not interested at all on what’s going on in there? Don’t you want to know why computers suck at math?

If the next coder you interview has trouble answering more than a couple of the following, that should be a no hire:
The next time you are interviewing a coder, here are some questions you could ask to probe their understanding of the binary system:

  • What are the first ten powers of two?
  • What is 0×7F in binary? In decimal?
  • What does two’s complement mean?
  • How would you extract the 8 low-order bits from a word?
  • How would you count the number of bits in a word?
  • What arithmetic operation does shifting right by one bit correspond to?

To me, total lack of understanding in this area is a big red flag and should cause a no hire. It means that you either haven’t been programming long enough to encounter these bit-level problems, or worse, you did encounter them but gave up for one reason or another.  It’s a real confidence booster to know that you can, if need be, go all the way down to the bit level to solve a problem or understand a bug.

It’s all ones and zeroes, right? The bits are your friends. How well do you know them?

If you liked this, click here to receive new posts in a reader.
You should also follow me on Twitter here.

{ 7 comments… read them below or add one }

hackerboss Tuesday, October 20, 2009 at 11:41

Do you speak binary? http://bit.ly/20OJVO

This comment was originally posted on Twitter

Crossbrowser Tuesday, October 20, 2009 at 14:34

As you said yourself, most people will never ever have to work at the bit-level, then why is it important to know this stuff? I can answer most of these questions, at least partially, but I wouldn’t be able to code anything that needs binary operations (from the top of my head, if I have access to the Internet that’s another story).

I think what’s most important is how the person answers, not what the answer is because I wouldn’t expect a lot of coders to know that. Furthermore, if I was interviewing for a job and was asked that, it would raise a red flag on my side, I wouldn’t want to work somewhere where they’re still doing bit operations.

Ville Laurikari Tuesday, October 20, 2009 at 16:05

Thanks for your comment – yes, learning about the bit-level is not necessary, but I still encourage you to learn it. It’s not that hard, and it can prove to be useful one day.

Part of my feeling towards this (and other similar skills) is 50% cognitive dissonance. I’ve programmed in assembler, twiddled the bits, learned C, learned Lisp and Haskell, and I feel all this made me a better programmer. It would be hard to admit that it was all a waste of time.

The other 50% is that there seems to be a strong correlation between the capability to get things done and the versatility of skills you possess as a programmer. It’s not really about any particular skill; it’s about the general interest towards the craft of programming, constantly learning more and more and more, and becoming a better and better and better programmer in the process.

As Andrew Hunt and David Thomas say in The Pragmatic Programmer:

Tip 8: “Invest Regularly in Your Knowledge Portfolio”

Some basic bit twiddling skills is a great candidate for the next thing to add in your knowledge portfolio.

I cannot resist closing with this famous quote from Robert A. Heinlein:

A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects.

Crossbrowser Tuesday, October 20, 2009 at 17:34

I understand that knowing binary is a good thing, what I disagree with is when you say “If the next coder you interview has trouble answering more than a couple of the following, that should be a no hire”. Many people have a no-hire checklist but if all those no-hire points were respected all the time, not many programmers would have a job.

Ville Laurikari Tuesday, October 20, 2009 at 18:44

Yeah, I agree, that sounds a bit steep. It’s the “total lack of understanding” which should cause the no hire flag to rise. I toned it down, but I maintain that all programmers should understand the binary system. It’s not hard, and it really helps you understand the world of computing a lot better.

Toni Kaikkonen Friday, January 15, 2010 at 20:58

Everyone who has been doing 8-bit coding as a kid should be able to count the powers of two up to 2^16 as easily as alphabets. Your post made me realize that for non-programmers those numbers are probably meaningless :)

Ville Laurikari Friday, January 15, 2010 at 21:42

Meaningless indeed. When I turned 32, it was tough trying to explain my mother why the cake had “0100000″ on it.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Additional comments powered by BackType

Previous post:

Next post: