John Rat
Work at RealNetworks.
Work at RealNetworks.
is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.
CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.
Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.
Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.
I won't write out the code (cuz I'm lazy) but I have a very different approach to this that would be much faster than the above methods - but there's definitely a memory trade off.
- John Rat September 02, 2007Preinitialize an array of length 256 bytes. Initialize each element of the array with the number of bytes for that index. For example:
array[0] = 0; // because '0' has 0 ON bits
array[1] = 1; // because '1' has 1 ON bit
array[2] = 1; // because '2' has 1 ON bit
array[3] = 2; // because '3' has 2 ON bits
...
array[255] = 8; // because '255' has 8 ON bits
Then take your 32 bit integer and break it into 4 8-bit pieces. Use each piece as an index into the above array. Add up the 4 values. Bam! You're done. You could also preinitialize an array of length 65536 and break your 32 bit integer into 2 16-bit pieces. Hell, you could preinitialize an array of length 4294967296 and just use the original number as an index into the array.