相关疑难解决方法(0)

使用C中的按位运算符检查数字是否为非零

x使用合法运算符检查数字是否为非零值!.

例如:isNonZero(3) = 1,isNonZero(0) = 0

法律行动: ~ & ^ | + << >>

  • 注意:只应使用按位运算符.if,else,for等不能使用.
  • 编辑1:运营商数量不应超过10.
  • Edit2:考虑大小为int4个字节.

int isNonZero(int x) {
return ???;
}
Run Code Online (Sandbox Code Playgroud)

使用!它将是微不足道的,但我们如何不使用!

c bit-manipulation

25
推荐指数
3
解决办法
3万
查看次数

Bit Twiddling Hacks:以明显的方式交错位

我对这个问题很感兴趣

交错位明显的方式

(来自http://graphics.stanford.edu/~seander/bithacks.html)

unsigned short x;   // Interleave bits of x and y, so that all of the
unsigned short y;   // bits of x are in the even positions and y in the odd;
unsigned int z = 0; // z gets the resulting Morton Number.

for (int i = 0; i < sizeof(x) * CHAR_BIT; i++) // unroll for more speed...
{
  z |= (x & 1U << i) << i | (y & 1U << …
Run Code Online (Sandbox Code Playgroud)

c bit-manipulation bit-shift

15
推荐指数
1
解决办法
9102
查看次数

标签 统计

bit-manipulation ×2

c ×2

bit-shift ×1