YP2.6 Operations on Bits—Part II: Logical Operations(双语)

来源:互联网 发布:淘宝镜子自拍模特技巧 编辑:程序博客网 时间:2024/05/25 19:58

from:

[1] Introduction to Computing System, from Bits and Gates to C and Beyond. Yale N. Patt and Sanjay J. Patel. McGraw-Hill Higher Education.(英)

[2] 计算系统基础_陈道蓄http://course.jingpinke.com/details?uuid=8a833999-2031c13b-0120-31c13bb1-012b

2.6 Operations on BitsPart II: Logical Operations

We have seen that it is possible to perform arithmetic (e.g., add, subtract) on values represented as binary patterns. Another class of operations that it is useful to perform on binary patterns is the set oflogicaloperations.

Logical operations operate on logical variables. A logical variable can have one of two values, 0 or 1. The namelogicalis a historical one; it comes from the fact that the two values 0 and 1 can represent the two logicalvalues falseand true, but the use of logical operations has traveled far from this original meaning.

There are several basic logic functions, and most ALUs perform all of them.

2.6.1 The AND Function

AND is a binary logical function. This means it requires two pieces of input data. Said another way, AND requires two source operands. Each source is a logical variable, taking the value 0 or 1. The output of AND is 1 only if both sources have the value 1. Otherwise, the output is 0. We can think of the AND operation as the ALL operation; that is, the output is 1 only if ALL two inputs are 1. Otherwise, the output is 0.

A convenient mechanism for representing the behavior of a logical operation is thetruth table.A truth table consists ofn + 1 columns and 2n rows. The first ncolumns correspond to thensource operands. Since each source operand is a logical variable and can have one of two values, there are2n unique values that these source operands can have. Each such set of values (sometimes called an input combination) is represented as one row of the truth table. The final column in the truth table shows the output for each input combination.

In the case of a two-input AND function, the truth table has two columns for source operands, and four (22) rows for unique input combinations.

   A B AND

0 0 0

0 1 0

1 0 0

1 1 1

We can apply the logical operation AND to two bit patterns of m bits each. This involves applying the operation individually to each pair of bits in the two source operands. For example, ifaandb in Example 2.6 are 16-bit patterns, then c is the AND ofaand b. This operation is often called a bit-wise AND

Example2.6

If c is the AND of a and b, where a = 0011101001101001 and b = 0101100100100001, what is c?

We form the AND of a and b by bit-wise ANDing the two values.

That means individually ANDing each pair of bits ai and bi to form ci. For example,

Since a0 = 1 and b0 = 1, c0 is the AND of a0 and b0, which is 1.

Since a6 = 1 and b6 = 0, c6 is the AND of a6 and b6, which is 0.

The complete solution for c is

 

a: 0011101001101001

b: 0101100100100001

c: 0001100000100001

Example2.6

Suppose we have an eight-bit pattern. let's call it A, in which the rightmost two bits have particular significance. The computer could be asked to do one of four tasks depending on the value stored in the two rightmost bits ofA.Can we isolate those two bits? 

Yes, we can, using a bit mask. Abit maskis a binary patten that enables the hits of A to be separated into two parts generally the part you care about and the part you wish to ignore. In this case, the bil mask 00000011 ANDed with A produces 0 in bit positions 7 through 2, and the original values of bits 1 and 0 ofA in hit positions 1 and 0. The bit mask is said tomask ourthe values in bit positions 7 through 2.

If A is 01010110. the AND ofA and the bit mask 00000011 is 00000010. If A is 11111100, the AND ofAand the bit mask 00000011 is 00000000.

That is. the result of ANDing any eight-bit pattern with the mask 00000011 is one of the four patterns 00000000, 00000001. 00000010, or 00000011. The result of ANDing with the mask is to highlight the two bits that relevant. 

2.6.2 The OR Function

OR is also a binary logical function. It requires two source operands, both of which are logical variables. The output of OR is 1 if any source has the value 1. Only if both sources are 0 is the output 0. We can think of the OR operation as the ANY operation; that is, the output is 1 if ANY of the two inputs are 1. The truth table for a two-input OR function is

A B OR

0 0 0

0 1 1

1 0 1

1 1 1

In the same way that we applied the logical operation AND to two m-bit patterns, we can apply the OR operation bit-wise to two m-bit patterns.

Example 2.8

If c is the OR of a and b. where a = 0011101001101001 andb = 0101100100100001. as before, what is c?

We form the OR of a and b by bit-wise ORing the two values. That means individually ORing each pair of hitsaiandbi to form ci.For example, sincea0 = 1 andb0 = 1,c0 is the OR ofa0 andb0, which is 1. Since a6 = 1 andb6 = 0, c6 is the OR ofa6 andb6. which is also 1.

The complete solution forc is

a: 0011101001101001

b: 0101100100100001

C: 0111101101101001

Sometimes this OR operation is referred to as theinclusive-OR in order to distinguish it from the exclusive-OR function, which we will discuss momentarily.

2.6.3 The NOT Function

NOT is a unary logical function. This means it operates ononly one source operand. It is also known as thecomplement operation. The output is formed by complementing the input. We sometimes say the output is formed byinverting the input. A 1 input results in a 0 output. A 0 input results in a 1 output.

The truth table for the NOT function is

                                                                                          A NOT A

 0 1

 1 0

In the same way that we applied the logical operation AND and OR to two m-bit patterns, we can apply the NOT operation bit-wise to one m-bit pattern. Ifais as before, thenc is the NOT of a.

a: 0011101001101001

c: 1100010110010110

2.6.4 The Exclusive-OR Function

Exclusive-OR, often abbreviated XOR, is abinary logical function. It, too, requires two source operands, both of which are logical variables. The output of XOR is 1 if the two sources are different. The output is 0 if the two sources are the same.

The truth table for the XOR function is

   A B XOR

0 0 0

0 1 1

1 0 1

1 1 0

In the same way that we applied the logical operation AND to two m-bit patterns, we can apply the XOR operation bit-wise to two m-bit patterns.

Example 2.9

If a and b are 16-bit patterns as before, thenc(shown here) is the XOR ofa andb.

a: 0011101001101001

b: 0101100100100001

C: 0110001101001000

Note the distinction between the truth table for XOR shown here and the truth table for OR shown earlier. In the ease of exclusive-OR, if both source operands are 1, the output is 0. That is. the output is 1 if the first operand is 1 but the second operand is not 1 or if the second operand is 1 but the first operand is not 1. The termexclusiveis used because the output is 1 ifonlyone of the two sources is 1. The OR function, on the other hand, produces an output 1 if only one of the two sources is 1, or if both sources are 1. Ergo,

the name inclusive-OR.

Example 2.10

Suppose we wish to know if two patterns arc identical. Since the XOR function produces a 0 only if the corresponding pair of bits is identical, two patterns are identical if the output of the XOR is all zeros.


2.6 比特运算—第二部分:逻辑运算

    在前面一部分,我们看到了对位组合做算术运算(比方说,加法,减法)。下面,介绍另一类位组合的运算,逻辑运算。

    逻辑运算是对逻辑变量做运算。一个逻辑变量可以为两个值之一,0或1“逻辑”这个名称来源于使用0和1表示逻辑值“假”和“真”,但是现在的逻辑运算已经与这个原始意义没有多大关系了。

下面的一些基本的逻辑函数,是大部分ALU都能进行的逻辑运算。

2.6.1 与函数(AND)

    与是一个二元函数,表示它需要两个输入的数据。换句话说,与函数需要两个源操作数,每个源操作数都是一个逻辑变量,值为0或1。只有当两个源操作数都是1时输出才为1,否则为0。我们可以把它假想成ALL运算,那就是说,只有两个输入都(ALL)为1时输出才为1,否则为0。

表示逻辑运算的一个方便的方法是使用真值表。一个真值表有n+1列和2n行。前面n列对应着n个源操作数。既然每个源操作数都是0或1中的一个,那么源操作数组合就有2n种可能。每组这样的值的组合(或称为输入组合)使用真值表的一行表示出来。真值表最后一列表示每种组合的输出。

在两个输入的与函数例子中,真值表有两列源操作数和四个输入组合:

A

B

AND

0

0

0

0

1

0

1

0

0

1

1

1

    我们可以把逻辑运算扩展为两个m位的位组合的运算。这就涉及到要对两个源操作数中对应位上的数字按位做与运算。例如a,b是16位的位组合,c是a和b做与运算的结果。这个操作通常叫做按位与运算。

例题2.6   

如果c是a和b做与运算的结果,此处a=0011101001101001,b=0101100100100001,则c是什么?

我们通过对两个值进行按位与运算,得到a和b做与运算的结果。

这意味着对每一对ai和bi分别进行与运算,得到ci。例如,既然a0=1,b0=1,那么c0是a0和b0进行与运算,结果为1。

既然a6=1,b6=0,那么c6是a6和b6进行与运算,结果为0。

对于c的解答为:

a:   0011101001101001

                                      b:    0101100100100001

                                      c:    0001100000100001

例题2.7  假设我们有一个8位的位组合,称为A,最右边的两位有特殊的重要性。根据存储在A中的最右边两位,计算机被要求处理4个任务之一。我们可以把这两位孤立出来吗?

使用位屏蔽,就可以做到这一点。位屏蔽是能够使A的8位分隔为两部分二进制组合,这两部分分别是你关心的一部分、以及你想忽略的一部分。在这个例子中,位屏蔽为00000011,它和A做与运算,从7位到2位的位置上都为0,而0位和1位中的原来的值还在0和1的位置上。位屏蔽则屏蔽了7位到2位上的值,孤立出有重要性的0位和1位。

如果A是01010110,A与位屏蔽00000011做与运算,得到00000010。如果A是11111100,A与位屏蔽00000011做与运算,得到00000000。

也就是说,位屏蔽00000011和任何8位的组合做与运算,结果将是四种组合之一,这四种组合是00000000,00000001,00000010或00000011。和位屏蔽进行与运算的结果是使相应的两位突出出来。

2.6.2 或函数(OR)

    或也是一个二元函数。它需要两个操作数,这两个操作数都是逻辑变量。如果两个操作数中有一个为1,那么或函数的输出就为1。当且仅当两个操作数都为0时,或函数的输出为0。我们可以把或运算看作是ANY运算;即只要两个输入中有一个值(ANY)为1,那么结果就为1。

    以下是一个两个输入的或函数的真值表。

A

B

OR

0

0

0

0

1

1

1

0

1

1

1

1

我们已对两个m位的位组合进行了逻辑与运算,那么用同样的方式,我们也可以对两个m位的位组合按位进行或运算。比如,如果a和b仍取以前的值,那么c就是a和b或运算的结果。

例题2.8 如果c是a和b做或运算的结果,和前面一样,a=0011101001101001,b=0101100100100001,则c是什么?

我们通过对两个值进行按位或运算,得到a和b做或运算的结果。这意味着对每一对ai和bi分别进行或运算,得到ci。例如,既然a0=1,b0=1,那么c0是a0和b0进行或运算,结果为1。既然a6=1,b6=0,那么c6是a6和b6进行或运算,结果也为1。

对于c的解答为:

a:   0011101001101001

                                      b:    0101100100100001

                                      c:    0111101101101001

有时这种或运算也被称为包含或运算,这样做是为了将它区别于异或函数,我们将在后面讨论异或函数。

2.6.3 非函数(NOT)

    非函数是一个一元函数,表明它只作用于一个操作数。其输出结果是通过对输入进行补运算,即取反操作得到,也被称作补运算。当输入为1 时,输出为0;输入为0时,结果为1。

    非函数的真值表表示如下。

A

NOT

1

0

0

1

采用对两个m位的位组合进行逻辑与和或运算相同的方式,我们对一个m位的位组合按位进行非运算。如果a仍是以前的值,那么c就是对a进行非运算的结果。

a:   0011101001101001

                                      c:    1100010110010110

2.6.4 异或函数

异或,通常简写成XOR,是一个二元函数。它也需要两个源操作数,这两个数都是逻辑变量。若两个源操作数不同则异或运算输出为1,否则为0。异或函数真值表如下: 

A

B

OR

0

0

0

0

1

1

1

0

1

1

1

0

采用与m位的位组合的逻辑与运算相同的方法,我们也可以对m位的位组合做异或运算。

例题2.9 假如a和b与前面例子相同,是16位的位组合,c是a和b的异或运算:

a: 0011101001101001

b: 0101100100100001

c: 0110001101001000

注意区别XOR真值表和OR真值表,在异或运算中,若两个源操作数都是1,那么结果为0,也就是说,当第一个数是1而第二个不是1,或者第二个数是1而第一个不是1时输出为1。用“异”这个字是因为只有当一个数是1而另一个不是1时输出才是1。另一方面,或运算是当两个数之一为1或两个数全为1时,输出为1,因此,或运算又称为“包含或”。 

例题2.10 判断两个位组合是否相同。

既然只有当相应的位上是相同的值时,XOR函数才得到0,那么如果异或函数的输出全为0,两个位组合就是相同的。

原创粉丝点击