2 ms·
Opening "Hacker's Delight", which you (as a C++ programmer) surely have in your collection next to "Code Complete 2", we find: "Apparently, many people have ma
by Jabbles 14y ago
Opening "Hacker's Delight", which you (as a C++ programmer) surely have in your collection next to "Code Complete 2", we find:
"Apparently, many people have made the mistake of assuming that a shift right signed of k positions divides a number by 2^k"
Surprisingly, the top few answers do not give the correction for negative numbers, which in "Hacker's Delight", is to add 2^k-1 to negative numbers before shifting.
Obviously, this ignores optimization and readability arguments, which although important, are probably less important than correctness.