引用计算不从 http://graphics.stanford.edu/~seander/bithacks.html分支的整数绝对值(abs)的代码:
int v; // we want to find the absolute value of v
unsigned int r; // the result goes here
int const mask = v >> sizeof(int) * CHAR_BIT - 1;
r = (v + mask) ^ mask;
专利变体:
r = (v ^ mask) - mask;
什么是 CHAR_BIT
? 如何使用它?