x使用合法运算符检查数字是否为非零值!.
例如:isNonZero(3) = 1,isNonZero(0) = 0
法律行动: ~ & ^ | + << >>
if,else,for等不能使用.int4个字节.int isNonZero(int x) {
return ???;
}
Run Code Online (Sandbox Code Playgroud)
使用!它将是微不足道的,但我们如何不使用!?
我对这个问题很感兴趣
交错位明显的方式
(来自http://graphics.stanford.edu/~seander/bithacks.html)
Run Code Online (Sandbox Code Playgroud)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 << …