1、2345Logical Operations at bit level6Figure 4.1 Logical operationsLogical Operations at bit level7Logical Operations at bit levelXNOT X0110Table 4.1 Truth table for NOT operationThe NOT operator takes one input and produces one output,which is the complement of the input.Table 4.1 shows the truth tab
2、le for the NOT operation at bit level.A truth table lists all possible input combinations along with the corresponding outputs.8Logical Operations at bit levelTable 4.2 Truth table for AND operationXYX AND Y0000101001119Logical Operations at bit levelInherent Rule of the AND Operator:The AND operati
3、on can be used to force(unset)individual bits in a bit string to 0.From Table 4.2,we can see that if one of the input bits is 0,the result is always 0 regardless of the other input bit,which means you dont need to check the corresponding bit value of the other input.10Logical Operations at bit level
4、Table 4.3 Truth table for OR operationXYX OR Y000011101111The OR operator accepts two inputs and creates one output.For each pair of input bits,the result is 0 if and only if both bits are 0;otherwise,the result is 1.Table 4.3 shows the truth table for the OR operation at bit level.11Logical Operati
5、ons at bit levelInherent Rule of the OR Operator:The OR operation can be used to force(set)individual bits in a bit string to 1.If one of the input bits is 1,the result is always 1 regardless of the other input bit,which means you dont need to check the corresponding bit value of the other input.12L
6、ogical Operations at bit levelTable 4.4 Truth table for XOR operationXYX XOR Y000011101110The XOR(exclusive OR)operator accepts two inputs and creates one output.For each pair of input bits,the result is 0 if and only if both bits are equal;otherwise,the result is 1.Table 4.4 shows the truth table f
7、or the XOR operation at bit level.13Logical Operations at bit level ()()x XOR yx AND NOT yORNOT x AND yThe XOR operator can be used to selectively invert(flip or NOT)bits in a bit string.If one of the input bits is 1,the result is always the inverse of the other input bit.If one of the input bits is
8、 0,the result is exactly the value of the other input bit.Actually,the XOR operator is not a basic operator.We can simulate its functionality by using the three basic operators.The following expression shows the relation between them:14Logical Operations at bit levelTable 4.5 Truth table for XNOR op
9、eratorXYX XNOR Y001010100111The XNOR operator accepts two inputs and creates one output.For each pair of input bits,the result is 0 if and only if both bits are not equal;otherwise,the result is 1.Table 4.5 shows the truth table for the XNOR operation at bit level.15Logical Operations at bit level (
10、)()()x XNOR yx AND y ORNOT x AND NOT yNOTx XOR yLike XOR operator,the XNOR operator can be represented by using the three basic operators.The following expression show the relation between them:It is not a basic logical operator too and it just a reverse(NOT)of XOR16Logical Operations at bit level x
11、 NAND yNOT x AND yx NOR yNOT x OR yThe NAND(or not-and)operator applies AND to its input bits and then inverts the output by applying NOT.For each pair of input bits,the result of applying NAND operator is 0 if and only if both bits are 1;otherwise,the result is 1.Similarly,the NOR(or not-or)operato
12、r applies OR to its input bits and then inverts the output by applying NOT.For each pair of input bits,the result of applying NOR operator is 0 if and only if either of its input bits are 1;otherwise,the result is 1.They can be represented through following expressions:So,they are not basic logical
13、operators either.17Logical Operations at bit levelTable 4.6 Truth table for NAND operatorXYX NAND Y001011101110The corresponding truth tables are shown in Table 4.6 and Table 4.718Logical Operations at bit levelTable 4.7 Truth table for NOR operatorXYX NOR Y00101010011019Logical Operations at patter
14、n levelThe logical operators that can be applied to an n-bit pattern arethe same as the logical operators applied to each individual inputbit.There are some examples to help us understand this rule.Apply the NOT operator on the bit pattern 1110101020Logical Operations at pattern levelApply AND,OR,XO
15、R,XNOR,NOR,and NAND operators on the bit patterns 10101101 and 0110101121Logical Operations at pattern levelApply AND,OR,XOR,XNOR,NOR,and NAND operators on the bit patterns 10101101 and 0110101122Logical Operations at pattern levelApply AND,OR,XOR,XNOR,NOR,and NAND operators on the bit patterns 1010
16、1101 and 0110101123Logical Operations at pattern levelApply AND,OR,XOR,XNOR,NOR,and NAND operators on the bit patterns 10101101 and 0110101124Logic Diagram Symbol25Logic Diagram SymbolTable 4.8 Logic Diagram Symbol and Logic Function Expression26Logic Diagram SymbolTable 4.8 Logic Diagram Symbol and
17、 Logic Function Expression27ApplicationsWe can modify a bit pattern with four binary logic operators:NOT,AND,OR,and XOR.These binary operators can complement,unset,set,or flip the target bit pattern with a mask.The mask is used to modify the target bit pattern.28ApplicationsThe most important applic
18、ation of AND operator is to unset(force to 0)specific bits in a target bit pattern.To do that,we must use an unsetting mask of the same length as of the target bit pattern.In this technique,to unset a specific bit of the target bit pattern,the corresponding bit in the mask is set to 0.Otherwise,to l
19、eave a bit in the target bit pattern unchanged,the corresponding bit in the mask is set to 1.For example,if you want to unset the 4 rightmost bits of an 8-bit pattern,the unsetting mask must be 11110000(Figure 4.2).29ApplicationsFigure 4.2 Example of unsetting specific bits of a bit pattern30Applica
20、tionsSimilarly,we use OR operator to set(force to 1)specific bits in a target bit pattern.In this technique,to set a specific bit of the target bit pattern,the corresponding bit in the setting mask is set to 1.Otherwise,to leave a bit in the target bit pattern unchanged,the corresponding bit in the
21、mask is set to 0.An example is shown in Figure 4.3.31ApplicationsFigure 4.3 Example of setting specific bits of a bit pattern32ApplicationsThe XOR operator is used to flip specific bits(0 becomes 1 and vice versa).The flip operation is functionally similar to the NOT operator.To flip a specific bit
22、in the target bit pattern,the corresponding bit in the flipping mask is set to 1.Otherwise,to leave a bit in the target bit pattern unchanged,the corresponding bit in the mask is set to 0.Figure 4.4 shows an example of flipping specific bits.33ApplicationsFigure 4.4 Example of flipping specific bits
23、34Applications 10011001 1011010100101100,00101100 1011010110011001,00101100 1001100110110101.aa XOR bXORba XOR bXORaa XOR bXOROther interesting applications of XOR operator are to do parity check of bit strings and to verify whether bytes sent across a network were corrupted and need retransmission,
24、or they were sent without errors.There are interesting applications of XOR operator.XOR can be used to exchange two numbers without using any intermediate variable.If we have a=10011001 and b=10110101,then after taking three XOR operations,we will finally exchange the value of a and b.353637Arithmet
25、ic operations on integers38Arithmetic operations on integers39Arithmetic operations on integers)1()(BABABA40Arithmetic operations on integers(+35)and(+29)in twos complement form are represented as 00100011 and 00011101 for an 8 bit memory location.The result remains same for any location size.The re
26、sult is 64 in decimal.41Arithmetic operations on integers A=(11100010)2 B=(00100010)2B B1022242Arithmetic operations on integers A=(11100010)2 B=(00100010)243Arithmetic operations on integers A=(11100010)2 B=(00100010)2244Arithmetic operations on integersInteger A=(+127)and B=(+3)are stored in twos
27、complement form.Show how B is added to A.A=(01111111)2,B=(00000011)2A is added to B and the result is stored in R.Obviously,we get an error here:A+B=R=-126(=01111110)130.We will discuss this issue in next section.45Arithmetic operations on integers46Arithmetic operations on integers122-11NNx1-N2-121
28、-N47Arithmetic operations on integersFigure 4.5 Overflow 1-82-121-848Arithmetic operations on integersAs we can see from Figure 4.6,when we reach the number 127 and add 3 to 127,the final result is-126 rather than 130.Figure 4.6 Twos Complement representation(clockwise)49Arithmetic operations on rea
29、ls1.Check the signs.a)If the signs are same,add the numbers and assign the common sign to the result.b)If the signs are different,take the absolute valuesof the numbers,subtract the smaller from the larger,and use the sign of the larger for the result.All arithmetic operations can be applied to real
30、s stored in floating-point format.In this section,we only show rules for addition and subtraction of reals.Addition and subtraction operation rules for floating-point number are basically the same.The basic steps are as follows:50Arithmetic operations on reals51Arithmetic operations on integers52Ari
31、thmetic operations on integersShow how the computer finds the result of(+21.75)+(+16.4375)=+38.1875.Because the exponents are the same,so we dont need to align.We apply addition operation on the combinations of sign and mantissa directly.Then we have following result:53Arithmetic operations on integ
32、ersShow how the computer finds the result of(+21.75)+(+16.4375)=+38.1875.Now the result needs to be normalized.We decrement the exponent once and shift the de-normalized mantissa to the left one position:So the final result is R=+2132127 1.001100011=+38.1875,as expected.54Arithmetic operations on re
33、als55Arithmetic operations on realsIn Figure 4.7,the fminN means the minimum positive number that a computer system can represent,which is a number very close to zero point.Then,the interval(-fminN,fminN),is called underflow gap.In early design of computer system,any number inside the underflow gap
34、would be set to zero.That kind of process is called“flush-to-zero underflow”.We usually use natural logarithm to avoid arithmetic underflow issues.Figure 4.7 Underflow gap 5657Logical shift operations58Logical shift operationsFigure 4.8 Logical shift operations59Logical shift operationsFigure 4.9 Ci
35、rcular shift operationsThe circular right-shift operation shifts every bit to the right.The leftmost bit replaced by the rightmost bit.The circular left-shift operation works conversely.Figure 4.9 shows these two operations on 8-bit patterns.60Logical shift operations61Logical shift operations62Logi
36、cal shift operations63Arithmetic shift operations64Arithmetic shift operationsFigure 4.10 Arithmetic shift operations65Arithmetic shift operationsNote that the MSB retains there.We can see that the result is-40,which is exactly the half value of original value,-80.66Arithmetic shift operations67 For more details about the subject discussed in this chapter,the following books are recommended.68697071