Great video, as always. I believe those are certainly neat tricks good to know, but IMHO I am not fond of it for an interview question: it depends too much on the previous knowledge of the particular trick and most of the time the compiler optimizes the naïve-but-readable version into something tuned for the target architecture. Just a couple of comments: The Hamming weight is so common that has been implemented as a single instruction in many modern CPU architectures under the name of "popcount", short for "population count". In GCC you may use __builtin_popcount to invoke it portably (with a default manual implementation if not supported). Also, std::popcount was introduced in C++20. See: en.wikipedia.org/wiki/Hamming_weight#Processor_support BTW, if you are interested in more bitwise wizardry I would recommend the book Matters Computational (free online: www.jjj.de/fxt/fxtbook.pdf ) and the classic Hacker's Delight. The disclaimer is valid anyways: a good compiler will, in many cases, do better than the "clever" solution. Also, the code in those books was highly performant 10 or 20 years ago and may not be so much with more recent compilers and architectures.
@DillonHuff
3 жыл бұрын
Thanks jp48! I recently had to find a new job, and during interview prep the n & (n - 1) trick was the bit twiddling hack I saw come up the most in bit-manipulation questions, though of course there are so many bit twiddling hacks that no single one of them is that common. These kinds of hacks are a fun way to show off for an interviewer, but as you point out, they often aren't the optimal way to do bit-manipulation on modern CPUs. I think we would both agree that the way to write low level bit-twiddling code that is used in the real world on modern CPUs is to either write easy-to-read code and leave optimization to the compiler, use existing standard librarys, or look for very high performance platform-specific ways to do the operation.
@japedr
3 жыл бұрын
Good luck with the interview (well, you won't need it :) I have to recognize I am a bit jealous of someone that has completed his PhD in Stanford, but you obviously worked hard and deserve it. I still recommend having a look at the books I mentioned.
@yurei_12
2 жыл бұрын
Excellent introduction! Thanks for sharing
@DillonHuff
2 жыл бұрын
Thank you Yu-Jui, glad you enjoyed it!
@ozne_2358
3 жыл бұрын
Thank you for the first trick : it was on the critical path of my neural processor !!
@DillonHuff
3 жыл бұрын
You're welcome. Glad it helped!
@larrymelman
Жыл бұрын
You should be really suspicious of an interviewer who ambushes you with "trivia" like this. It raises a red flag that he/she would be damn near impossible to work with. A professional interviewer will try to find out how you approach a problem. The answer to this question is "I'd google it". I would not waste valuable brain space with stuff like this.
Пікірлер: 8