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

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

问题描述

我一直在阅读有关的Objective-C位运营商在科昌的书,编程在Objective-C。

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

我很困惑这个部分,我虽然真正了解最一切presented我迄今。

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;

分配到W3 W1按位与常数3。这与运算值具有比最右边的两个比特为0,preserving从W1最右边的两个比特相同在w中设定的所有位的ffect,其他。

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;

此外,它具有所有,但字的最右边的四个比特设置为0。当在执行位操作使用的常数的效果,它通常更方便的是前preSS在任一进制或十六进制常数。

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.

好了,这就是我试图理解。现在,我用pretty多这整个概念极其混乱,我只是寻找一个澄清一点,如果有人愿意帮我出这一点。

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的第2个基地,换言之,二进制?

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?

谢谢!

推荐答案

数字可以是二进制pssed这样的前$ P $:

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.

按位与指取两个数字,他们排队在彼此的顶部,并创建一个具有1新号码,其中两个数字有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,其中任一号码有1一个新的号码(一切为0)。

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

按位XOR(异或)指取两个数字,它们排队在彼此的顶部,并创建具有一个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  

按位也(不用or)指取两个数字的按位或,然后反转一切(那里有一个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).

按位NAND(不和)指取按位两个数的和,再反向一切(那里有一个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).

持续:为什么字放大器; = 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且a = 0 0且B = 0 ... j和1 = j的 i和1 = I ,...)

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

这有什么用?在许多语言中,我们用的东西叫位掩码。位掩码本质上是一个数字,再presents一大堆小的数字结合在一起的。我们可以结合起来使用或数字,并使用它们拉开和。例如:

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;

如果我只有地图和帽子,我可以前preSS,作为 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\n");
} else {
  printf("I don't have a wand\n");
}

明白了吗?

编辑:更多的东西。

您还会遇到了bitshift运营商:LT;<和>>。这也就意味着转向左边的一切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 LT;&LT; 3 = 0001 LT;&LT; 3 = 0001000 = 8

1 << 3 = 0001 << 3 = 0001000 = 8

8 GT;&GT; 2 = 01000&GT;&GT; 2 = 010 = 2

8 >> 2 = 01000 >> 2 = 010 = 2

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

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