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

和 XOR 不 或 左/右转


当前回答

Bitwise operators are useful for looping arrays which length is power of 2. As many people mentioned, bitwise operators are extremely useful and are used in Flags, Graphics, Networking, Encryption. Not only that, but they are extremely fast. My personal favorite use is to loop an array without conditionals. Suppose you have a zero-index based array(e.g. first element's index is 0) and you need to loop it indefinitely. By indefinitely I mean going from first element to last and returning to first. One way to implement this is:

int[] arr = new int[8];
int i = 0;
while (true) {
    print(arr[i]);
    i = i + 1;
    if (i >= arr.length) 
        i = 0;
}

这是最简单的方法,如果你想避免if语句,你可以像这样使用模方法:

int[] arr = new int[8];
int i = 0;
while (true) {
    print(arr[i]);
    i = i + 1;
    i = i % arr.length;
}

这两种方法的缺点是,模运算符是昂贵的,因为它在整数除法后寻找余数。第一个方法在每次迭代中运行if语句。然而,如果你的数组长度是2的幂,你可以很容易地生成一个像0 ..长度- 1,使用&(位和)操作符,如I & Length。知道了这些,上面的代码就变成了

int[] arr = new int[8];
int i = 0;
while (true){
    print(arr[i]);
    i = i + 1;
    i = i & (arr.length - 1);
}

下面是它的工作原理。在二进制格式中,所有2的幂减去1的数都只用1表示。例如,二进制的3是11,7是111,15是1111,等等,你懂的。现在,如果你用任意一个数对一个只由1组成的二进制数,会发生什么?假设我们这样做:

num & 7;

如果num小于或等于7,那么结果将是num,因为每个加1的&-ed就是它自己。如果num大于7,在&操作期间,计算机将考虑7的前导零,当然,在&操作后,这些前导零将保持为零,只有后面的部分将保留。比如二进制的9和7

1001 & 0111

结果将是0001,它是十进制的1,并定位数组中的第二个元素。

其他回答

& =和: 屏蔽掉特定的位。 您正在定义应该显示的特定位 或者不显示。0x0 & x将清除字节中的所有位,而0xFF不会改变x。 0x0F将显示较低位置的位。

转换: 要将较短的变量转换为具有位标识的较长的变量,必须调整位,因为int类型中的-1是0xFFFFFFFF,而long类型中的-1是0xffffffffffffffffff。为了保护 转换后应用掩码的标识。

| =或 位设置。如果已经设置了位,则位将独立设置。许多数据结构(位字段)有IS_HSET = 0, IS_VSET = 1这样的标志,可以独立设置。 要设置标志,您应用IS_HSET | IS_VSET(在C和汇编中,这是非常方便阅读的)

^ = XOR 找出相同或不同的部分。

~ =不 比特翻转。

可以证明,所有可能的局部位操作都可以通过这些操作来实现。 如果你愿意,你可以通过位操作来实现ADD指令。

以下是一些妙招:

http://www.ugcs.caltech.edu/~wnoise/base2.html http://www.jjj.de/bitwizardry/bitwizardrypage.html

我们使用位标记,使会话较小的登录权限在我们的内部网站。

我见过它们在基于角色的访问控制系统中使用。

Bit fields (flags) They're the most efficient way of representing something whose state is defined by several "yes or no" properties. ACLs are a good example; if you have let's say 4 discrete permissions (read, write, execute, change policy), it's better to store this in 1 byte rather than waste 4. These can be mapped to enumeration types in many languages for added convenience. Communication over ports/sockets Always involves checksums, parity, stop bits, flow control algorithms, and so on, which usually depend on the logic values of individual bytes as opposed to numeric values, since the medium may only be capable of transmitting one bit at a time. Compression, Encryption Both of these are heavily dependent on bitwise algorithms. Look at the deflate algorithm for an example - everything is in bits, not bytes. Finite State Machines I'm speaking primarily of the kind embedded in some piece of hardware, although they can be found in software too. These are combinatorial in nature - they might literally be getting "compiled" down to a bunch of logic gates, so they have to be expressed as AND, OR, NOT, etc. Graphics There's hardly enough space here to get into every area where these operators are used in graphics programming. XOR (or ^) is particularly interesting here because applying the same input a second time will undo the first. Older GUIs used to rely on this for selection highlighting and other overlays, in order to eliminate the need for costly redraws. They're still useful in slow graphics protocols (i.e. remote desktop).

这些只是我最先想到的几个例子——这不是一个详尽的清单。

按位&用于屏蔽/提取字节的某一部分。

1字节变量

 01110010
&00001111 Bitmask of 0x0F to find out the lower nibble
 --------
 00000010

特别是移位运算符(<< >>)经常用于计算。