【发布时间】:2021-03-21 20:00:24
【问题描述】:
我一直在审查位操作并一直在查看此页面: http://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs
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;
int 的大小是 4,字节中的字符是 8。所以基本上我认为这是 32 位整数的掩码。 我理解对于负 v 这应该创建一个 -1 的掩码,对于正数,一个掩码 0. 我的问题是为什么只移动 28 位而不是 31 位?感觉好像我在这里遗漏了一些东西。
【问题讨论】:
标签: c bit-manipulation