了解按位与运算符 [英] Understanding the bitwise AND Operator

查看:40
本文介绍了了解按位与运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在 Kochan 的书Objective-C 中的编程"中阅读有关 Objective-C 中的位运算符的内容.

I have been reading about bit operators in Objective-C in Kochan's book, "Programming in Objective-C".

我对这部分感到非常困惑,尽管到目前为止我已经真正理解了呈现给我的大多数其他内容.

I am VERY confused about this part, although I have really understood most everything else presented to me thus far.

这是书中引述:

按位与运算符

按位与常用于掩码操作.也就是说,这个运算符可以很容易地用于将数据项的特定位设置为 0.例如,语句

Bitwise ANDing is frequently used for masking operations. That is, this operator can be used easily to set specific bits of a data item to 0. For example, the statement

w3 = w1 & 3;

将 w1 的值与常量 3 按位与的结果分配给 w3.这与设置 w 中所有位的效果相同,除了最右边的两位为 0 并保留 w1 中最右边的两位.

assigns to w3 the value of w1 bitwise ANDed with the constant 3. This has the same ffect of setting all the bits in w, other than the rightmost two bits to 0 and preserving the rightmost two bits from w1.

与 C 中的所有二进制算术运算符一样,二进制位运算符也可以通过添加等号用作赋值运算符.声明

As with all binary arithmetic operators in C, the binary bit operators can also be used as assignment operators by adding an equal sign. The statement

word &= 15;

因此执行与以下相同的功能:

therefore performs the same function as the following:

word = word & 15;

此外,它还具有将 word 的最右边四位以外的所有位设置为 0 的效果.在执行按位运算时使用常量时,通常以八进制或十六进制表示法来表示常量更方便.

Additionally, it has the effect of setting all but the rightmost four bits of word to 0. When using constants in performing bitwise operations, it is usually more convenient to express the constants in either octal or hexadecimal notation.

好的,这就是我想要理解的.现在,我对整个概念非常困惑,如果有人愿意帮助我解决这个问题,我只是想澄清一下.

OK, so that is what I'm trying to understand. Now, I'm extremely confused with pretty much this entire concept and I am just looking for a little clarification if anyone is willing to help me out on that.

当这本书现在提到设置所有位"时,所有位.. 到底什么是位.这不就是二阶基数的 0 或 1,换句话说,是二进制的吗?

When the book references "setting all the bits" now, all of the bits.. What exactly is a bit. Isn't that just a 0 or 1 in 2nd base, in other words, binary?

如果是这样,为什么在第一个例子中,除了最右边的 2"之外的所有位都是 0?它是 2,因为它是 3 - 1,从我们的常数中取 3?

If so, why, in the first example, are all of the bits except the "rightmost 2" to 0? Is it 2 because it's 3 - 1, taking 3 from our constant?

谢谢!

推荐答案

数字可以这样用二进制表示:

Numbers can be expressed in binary like this:

3    = 000011
5    = 000101
10   = 001010

...等等.我假设您熟悉二进制.

...etc. I'm going to assume you're familiar with binary.

按位 AND 的意思是取两个数字,将它们排成一行,然后创建一个新的数字,其中两个数字都是 1(其他都是 0).

Bitwise AND means to take two numbers, line them up on top of each other, and create a new number that has a 1 where both numbers have a 1 (everything else is 0).

例如:

    3          =>  00011
  & 5          =>  00101
------           -------
    1              00001

按位或意味着取两个数字,将它们排成一行,然后创建一个新的数字,其中一个数字为 1(其他数字均为 0),该数字为 1.

Bitwise OR means to take two numbers, line them up on top of each other, and create a new number that has a 1 where either number has a 1 (everything else is 0).

例如:

    3          =>  00011
  | 5          =>  00101
------           -------
    7              00111

按位异或(异或)意味着取两个数字,将它们排成一行,然后创建一个有 1 的新数字,其中一个数字为 1,另一个数字为 0(其他一切都是0).

Bitwise XOR (exclusive OR) means to take two numbers, line them up on top of each other, and create a new number that has a 1 where either number has a 1 AND the other number has a 0 (everything else is 0).

例如:

    3          =>  00011
  ^ 5          =>  00101
------           -------
    6              00110  

按位 NOR(非或)表示对两个数进行按位或运算,然后反转所有内容(原来是 0,现在是 1,以前是 1,现在是 0).

Bitwise NOR (Not OR) means to take the Bitwise OR of two numbers, and then reverse everything (where there was a 0, there's now a 1, where there was a 1, there's now a 0).

按位与非(非与)表示取两个数的按位与,然后反转所有内容(有 0 的地方,现在有 1,有 1 的地方,现在有 0).

Bitwise NAND (Not AND) means to take the Bitwise AND of two numbers, and then reverse everything (where there was a 0, there's now a 1, where there was a 1, there's now a 0).

继续:为什么word &= 15 将除最右边的 4 位之外的所有位都设置为 0?你现在应该可以弄清楚了......

Continuing: why does word &= 15 set all but the 4 rightmost bits to 0? You should be able to figure it out now...

     n          =>  abcdefghjikl
  & 15          =>  000000001111
------            --------------
     ?              00000000jikl

(0 AND a = 0, 0 AND b = 0, ... j AND 1 = j, iAND 1 = i, ...)

(0 AND a = 0, 0 AND b = 0, ... j AND 1 = j, i AND 1 = i, ...)

这有什么用?在许多语言中,我们使用称为位掩码"的东西.位掩码本质上是一个数字,表示组合在一起的一大堆较小的数字.我们可以使用 OR 将数字组合在一起,并使用 AND 将它们分开.例如:

How is this useful? In many languages, we use things called "bitmasks". A bitmask is essentially a number that represents a whole bunch of smaller numbers combined together. We can combine numbers together using OR, and pull them apart using AND. For example:

int MagicMap = 1;
int MagicWand = 2;
int MagicHat = 4;

如果我只有地图和帽子,我可以将其表示为 myInventoryBitmask = (MagicMap | MagicHat),结果就是我的位掩码.如果我什么都没有,那么我的位掩码是 0.如果我想看看我是否有我的魔杖,那么我可以这样做:

If I only have the map and the hat, I can express that as myInventoryBitmask = (MagicMap | MagicHat) and the result is my bitmask. If I don't have anything, then my bitmask is 0. If I want to see if I have my wand, then I can do:

int hasWand = (myInventoryBitmask & MagicWand);
if (hasWand > 0) {
  printf("I have a wand
");
} else {
  printf("I don't have a wand
");
}

明白了吗?

更多内容

您还会遇到bitshift"运算符:<<和>>.这只是意味着将所有内容左移 n 位"或将所有内容右移 n 位".

You'll also come across the "bitshift" operator: << and >>. This just means "shift everything left n bits" or "shift everything right n bits".

换句话说:

<代码>1 <<<3 = 0001 <<<3 = 0001000 = 8

还有:

<代码>8 >>2 = 01000 >>2 = 010 = 2

这篇关于了解按位与运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆