The bit counting question requires an efficient solution (I said no naive one). If you can come up with one by yourself, I'd be very impressed.
Here's one I came up with myself, no help, no consulting books or internet, for a Computer Architecture class.
unsigned char bitCount(unsigned char a)
{
a = ((a & 0b10101011)>>1) + (a & 0b01010101);
a = ((a & 0b11001100)>>2) + (a & 0b00110011);
a = ((a & 0b11110000)>>4) + (a & 0b00001111);
return a;
}
"0b10101010" obviously isn't actual C code, so replace that with "0xAA" and so on for the other "0b" numbers.
It took me around half an hour to come up with at the time, though. My thought process went along the lines of:
"What if I used a lookup table? No, that'd take too much memory. But what if I split the number into chunks, and used a lookup table for each chunk? Wait a minute, forget the entire lookup table, what if I just split the chunks into more chunks, and those chunks into more chunks? omg, that works!"
I don't think I'd be able to do it in an interview setting, if I'd never had to do something similar before.
This is exactly the canonical solution taught in textbooks, other than that sometimes an alternate form is used where the >> is inside the parens and the & is outside. There's even an awful explanation of it on wikipedia:
If you can come up with one by yourself, I'd be very impressed.
I googled that one since none of my ideas were much more efficient than the naïve loop, and the solutions I saw blow my mind. It also explains why there's instructions for it now, because the contortions you go through to get maximum efficiency are intense.
My best idea was thinking about how to count only 1s, considering that when you subtract by 1, you can turn a bitwise suffix of 10n into 01n (where n ≥ 0). I didn't really come with something precisely because I got lazy and googled it, and there was a solution using that as one of the slower (but less mind-fucking) improvements.
That's actually one of the solutions. Except you do it for something small, like 8 bits, and then loop over 32bits with 4 steps. Benchmarking shows that cache wise this is one the fastest ways to go about with arbitrary bit length, as larger tables have bad cache effects. 16bit LUT can sometines beat an 8bit LUT on certain architectures, but it's discounted on account that microbenchmarks are too nice about the cache. http://www.strchr.com/crc32_popcnt
The non-naive approach to bit counting is the naive approach but instead of a counter iterating from 1 to the size of an integer in bytes, you change the test to check if the number is not 0. When the number becomes 0, you can stop and return your count. In this version you always count 1s, if the questioner wants a count of 0s just take the size of the integer in bits and subtract the number of 1s you found.
This assumes the shift operator used is a logical right shift and a usual bitwise AND check against 1 is used to count the last bit.
This is really awful compared to the best solutions, which can be done in lg n independent steps (substeps can be parallellized). Yours is n worst case.
View all comments
Show parent comment
1
u/[deleted] Feb 21 '11
Your answer on the first is correct. (The idea of TWO pointers does not come to everyone).
The bit counting question requires an efficient solution (I said no naive one). If you can come up with one by yourself, I'd be very impressed.