下面的位运算符在现实世界中有哪些用例?

和 XOR 不 或 左/右转


当前回答

我使用它们作为选项处理程序,例如在访问控制列表中描述特定的资源。

看看这篇文章http://planetozh.com/blog/2006/05/php-bitwise-operators-example-of-use/

Edit:

还有一个链接: http://blog.code-head.com/how-to-write-a-permission-system-using-bits-and-bitwise-operations-in-php

其他回答

河内塔线性解采用位运算来解决问题。

public static void linear(char start, char temp, char end, int discs)
{
    int from,to;
    for (int i = 1; i < (1 << discs); i++) {
        from = (i & i-1) % 3;
        to = ((i | i-1) + 1) % 3;
        System.out.println(from+" => "+to);
    }
}

这个解决方案的解释可以在这里找到

我曾在一些游戏开发书籍中看到它是一种更有效的乘除方法。

2 << 3 == 2 * 8 
32 >> 4 == 32 / 16

您可以使用它们作为一种快速而不常用的散列数据的方法。

int a = 1230123;
int b = 1234555;
int c = 5865683;
int hash = a ^ b ^ c;

加密都是按位操作。

我很惊讶,没有人为互联网时代选择一个显而易见的答案。计算子网的有效网络地址。

http://www.topwebhosts.org/tools/netmask.php