How do you end an email exchange?
One unexpected (by me, at least) byproduct of our technological age is the never-ending email conversation. I'm not sure if it's the desire to have the last word, or simply the feeling that leaving someone hanging is rude, but some email conversations go on for days, weeks, even months after their actual content has ended. (Some, of course, never had any actual content to begin with.)When your on the phone, you're talking in real time, which requires some amount of concentration. You can end a phone conversation because talking on the phone is a physical committment. Sure, you may have enough attention left over to cook, grimace to show others in the room your i
Bit Counting (The end, I hope)
So the original question was, "How do you write a function to count the number of bits set in an arbitrary byte?" I figured that if you're going to do this repeatedly, it might be worth just building a lookup table. The question then becomes "How do you populate a table of set bit counts for each possible byte?" This can be solved with a simple iteration:int exp = 1;table[0] = 0;for (int i = 1; i < 256; i++){ if (i == exp*2) exp *= 2; table[i] = table[i-exp] + 1;} Now if I had thought of that during the interview ...
Things to hate
People who insist on adding milk and sugar to their coffee while it's sitting under the coffee maker, so that my mug gets covered with the milk and sugar they've spilled.Coffee makers in general, for that matter.
Bit Counting (Continued)
So here's the pattern. You're trying to populate a table so that each entry in the table contains the number of bits set in a one byte binary representation of that entry's index:0 = 00000000 = 0 bits set1 = 00000001 = 1 bit set2 = 00000010 = 1 bit set3 = 00000011 = 2 bits set4 = 00000100 = 1 bit set5 = 00000101 = 2 bits set6 = 00000110 = 2 bits set7 = 00000111 = 3 bits set8 = 00001000 = 1 bit setand so on.So the first two are obviously 0 and 1. The next two are the same, but with the 00000010 bit set. Then the next four follow the same pattern but with the 00000100 bit set. The next eight follow the pattern o
Bit Counting
A few years ago, I was on a job interview, and the interviewer asked me how to write a function to count the number of '1' bits in a byte. I replied that this sounded like an obvious table lookup problem. Just create the table with 256 entries, and use the byte value itself to index to the correct bit count.That seemed to take him a little off guard, but he quickly asked how to populate the table. He was still trying to get me to answer his original question with the explanation of how to test each bit by shifting the byte successively and ANDing with 1. If the shifted byte ANDed with 1 was 1, then the low bit was on, and the bit count for that byte should be incremented.
Not enough data.
Calculated for blogs with 20+ followers.
Questions? contact: networkedblogs@ninua.com
Copyright (C) 2008, Ninua, Inc.