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

和 XOR 不 或 左/右转


当前回答

下面是一些处理将标志存储为单个位的常见习惯用法。

enum CDRIndicators {
  Local = 1 << 0,
  External = 1 << 1,
  CallerIDMissing = 1 << 2,
  Chargeable = 1 << 3
};

unsigned int flags = 0;

设置Chargeable标志:

flags |= Chargeable;

清除CallerIDMissing标记:

flags &= ~CallerIDMissing;

测试CallerIDMissing和Chargeable是否设置:

if((flags & (CallerIDMissing | Chargeable )) == (CallerIDMissing | Chargeable)) {

}

其他回答

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).

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

它们主要用于位操作(惊喜)。下面是在PHP代码库中找到的一些实际示例。

字符编码:

if (s <= 0 && (c & ~MBFL_WCSPLANE_MASK) == MBFL_WCSPLANE_KOI8R) {

数据结构:

ar_flags = other->ar_flags & ~SPL_ARRAY_INT_MASK;

数据库驱动程序:

dbh->transaction_flags &= ~(PDO_TRANS_ACCESS_MODE^PDO_TRANS_READONLY);

编译器实现:

opline->extended_value = (opline->extended_value & ~ZEND_FETCH_CLASS_MASK) | ZEND_FETCH_CLASS_INTERFACE;

& =和: 屏蔽掉特定的位。 您正在定义应该显示的特定位 或者不显示。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

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

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

下面是一些处理将标志存储为单个位的常见习惯用法。

enum CDRIndicators {
  Local = 1 << 0,
  External = 1 << 1,
  CallerIDMissing = 1 << 2,
  Chargeable = 1 << 3
};

unsigned int flags = 0;

设置Chargeable标志:

flags |= Chargeable;

清除CallerIDMissing标记:

flags &= ~CallerIDMissing;

测试CallerIDMissing和Chargeable是否设置:

if((flags & (CallerIDMissing | Chargeable )) == (CallerIDMissing | Chargeable)) {

}