📄 ch4.htm
字号:
</TABLE></CENTER><P><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>You might be tempted to try the following:</BLOCKQUOTE><BLOCKQUOTE><TT>if ($firstVar == (9 || 10)) {<BR> print("Error!");<BR>};</TT></BLOCKQUOTE><BLOCKQUOTE>to determine <TT>if $firstVar</TT> is equal to either 9 or 10. Don't do it. Perl doesn't work this way. First, the expression (<TT>9 </TT><FONT FACE="Palatino">||</FONT><TT> 10</TT>) will be evaluated to be equal to 9. And then, Perl will evaluate <TT>$firstVar == 9</TT>. The correct method for testing <TT>$firstVar</TT> is to explicitly state each sub-condition that needs to be met in order for the entire condition to return true. The correct way is:</BLOCKQUOTE><BLOCKQUOTE><TT>if ($firstVar == 9 || $firstVar == 10) {<BR> print("Error!");<BR>};</TT></BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="ExampleThequotNOTquotOperator"><FONT SIZE=5 COLOR=#FF0000>Example: The "NOT" Operator (!)</FONT></A></H2><P>The <TT>!</TT> operator is used toconvert true values to false and false values to true. In otherwords, it inverts a value. Perl considers any non-zero value tobe true-even string values. Table 4.7 shows the results of usingthe <TT>!</TT> operator on true andfalse values.<BR><P><CENTER><B>Table 4.7 The ! Result Table</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=91><CENTER><I>Op1</I></CENTER></TD><TD WIDTH=108><CENTER><I>!Op1</I></CENTER></TD></TR><TR><TD WIDTH=91><CENTER>0</CENTER></TD><TD WIDTH=108><CENTER>1</CENTER></TD></TR><TR><TD WIDTH=91><CENTER>1</CENTER></TD><TD WIDTH=108><CENTER>0</CENTER></TD></TR></TABLE></CENTER><P><P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign a value of 10 to </I><TT><I>$firstVar</I></TT><I>.<BR>Negate </I><TT><I>$firstVar-!10</I></TT><I>is equal to </I><TT><I>0-</I></TT><I>andassign the new value to </I><TT><I>$secondVar</I></TT><I>.<BR>If the </I><TT><I>$secondVar</I></TT><I>variable is equal to zero, then print the string "zero."</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 10;$secondVar = !$firstVar;if ($secondVar == 0) { print("zero\n");};</PRE></BLOCKQUOTE><P>The program produces the following output:<BLOCKQUOTE><PRE>zero</PRE></BLOCKQUOTE><P>You could replace the 10 in the first line with "ten,"'ten,' or any non-zero, non-null value.<H2><A NAME="TheBitwiseOperators"><FONT SIZE=5 COLOR=#FF0000>The Bitwise Operators</FONT></A></H2><P>The <I>bitwise </I>operators, listed in Table 4.8, are similarto the logical operators, except that they work on a smaller scale.<BR><P><CENTER><B>Table 4.8 The Bitwise Operators</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=127><CENTER><I>Operator</I></CENTER></TD><TD WIDTH=463><I>Description</I></TD></TR><TR><TD WIDTH=127><CENTER><TT>op1 & op2</TT></CENTER></TD><TD WIDTH=463>The AND operator compares two bits and generates a result of 1 if both bits are 1; otherwise, it returns 0.</TD></TR><TR><TD WIDTH=127><CENTER><TT>op1</TT> | <TT>op2</TT></CENTER></TD><TD WIDTH=463>The OR operator compares two bits and generates a result of 1 if the bits are complementary; otherwise, it returns 0.</TD></TR><TR><TD WIDTH=127><CENTER><TT>op1</TT><I> ^ </I><TT>op2</TT></CENTER></TD><TD WIDTH=463>The EXCLUSIVE-OR operator compares two bits and gener-ates a result of 1 if either or both bits are 1; otherwise, it returns 0.</TD></TR><TR><TD WIDTH=127><CENTER><TT>~op1</TT></CENTER></TD><TD WIDTH=463>The COMPLEMENT operator is used to invert all of the bits of the operand. I've never found this useful, so we'll skip looking at an example of it.</TD></TR><TR><TD WIDTH=127><CENTER><TT>op1 >> op2</TT></CENTER></TD><TD WIDTH=463>The SHIFT RIGHT operator moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. Each move to the right effectively divides <TT>op1</TT> in half.</TD></TR><TR><TD WIDTH=127><CENTER><TT>op1 << op2</TT></CENTER></TD><TD WIDTH=463>The SHIFT LEFT operator moves the bits to the left, discards the far left bit, and assigns the rightmost bit a value of 0. Each move to the left effectively multiplies <TT>op1</TT> by 2.</TD></TR></TABLE></CENTER><P><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>Both operands associated with the bitwise operator must be integers.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>Bitwise operators are used to change individual bits in an operand.A single byte of computer memory-when viewed as 8 bits-can signifythe true/false status of 8 flags because each bit can be usedas a boolean variable that can hold one of two values: true orfalse. A <I>flag</I> variable is typically used to indicate thestatus of something. For instaNCe, computer files can be markedas read-only. So you might have a <TT>$fReadOnly</TT>variable whose job would be to hold the read-only status of afile. This variable is called a flag variable because when <TT>$fReadOnly</TT>has a true value, it's equivalent to a football referee throwinga flag. The variable says, "Whoa! Don't modify this file."<P>When you have more than one flag variable, it might be more efficientto use a single variable to indicate the value of more than oneflag. The next example shows you how to do this.<H3><A NAME="ExampleUsingtheampandOperators">Example: Using the &, |, and ^ Operators</A></H3><P>The first step to using bitwise operators to indicate more thanone flag in a single variable is to define the meaning of thebits that you'd like to use. Figure 4.1 shows an example of 8bits that could be used to control the attributes of text on adisplay.<P><A HREF="f4-1.gif"><B>Figure 4.1 : </B><I>The bit definition of a text attribute controlvariable</I>.</A><P>If you assume that <TT>$textAttr</TT>is used to control the text attributes, then you could set theitalic attribute by setting <TT>$textAttr</TT>equal to 128 like this:<BLOCKQUOTE><PRE>$textAttr = 128;</PRE></BLOCKQUOTE><P>because the bit pattern of 128 is 10000000. The bit that is turnedon corresponds to the italic position in <TT>$textAttr</TT>.<P>Now let's set both the italic and underline attributes on at thesame time. The underline value is 16, which has a bit patternof 00010000. You already know the value for italic is 128. Sowe call on the <TT>OR</TT> operatorto combine the two values.<BLOCKQUOTE><PRE>$textAttr = 128 </FONT><FONT SIZE=2 FACE="Palatino">|</FONT><FONT SIZE=2 FACE="Courier"> 16;</PRE></BLOCKQUOTE><P>or using the bit patterns (this is just an example-you can't dothis in Perl)<BLOCKQUOTE><PRE>$textAttr = 10000000 </FONT><FONT SIZE=2 FACE="Palatino">|</FONT><FONT SIZE=2 FACE="Courier"> 00010000;</PRE></BLOCKQUOTE><P>If you look back at Table 4.8 and evaluate each bit, you willsee that <TT>$textAttr</TT> gets assigneda value of 144 (or 10010000 as a bit pattern). This will set bothitalic and underline attributes on.<P>The next step might be to turn the italic attribute off. Thisis done with the EXCLUSIVE-OR operator, like so:<BLOCKQUOTE><PRE>$textAttr = $textAttr ^ 128;</PRE></BLOCKQUOTE><H3><A NAME="ExampleUsingthegtgtandltltOperators">Example: Using the >> and << Operators</A></H3><P>The <I>bitwise shift </I>operators are used to move all of thebits in the operand left or right a given number of times. Theycome in quite handy when you need to divide or multiply integervalues.<P>This example will divide by 4 using the <TT>>></TT>operator.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign a value of 128 to the </I><TT><I>$firstVar</I></TT><I>variable.<BR>Shift the bits inside </I><TT><I>$firstVar</I></TT><I>two places to the right and assign the new value to </I><TT><I>$secondVar</I></TT><I>.<BR>Print the </I><TT><I>$secondVar</I></TT><I>variable.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 128;$secondVar = $firstVar >> 2;print("$secondVar\n");</PRE></BLOCKQUOTE><P>The program produces the following output:<BLOCKQUOTE><PRE>32</PRE></BLOCKQUOTE><P>Let's look at the bit patterns of the variables before and afterthe shift operation. First, <TT>$firstVar</TT>is assigned 128 or 10000000. Then, the value in <TT>$firstVar</TT>is shifted left by two places. So the new value is 00100000 or32, which is assigned to <TT>$secondVar</TT>.<P>The rightmost bit of a value is lost when the bits are shiftedright. You can see this in the next example.<P>This example will divide by 8 using the <TT>>></TT>operator.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign a value of 129-a bit pattern of 10000001-to </I><TT><I>$firstVar</I></TT><I><FONT FACE="MCPdigital-BI">.</FONT>Every odd value has the rightmost bit set.<BR>Shift the bits inside </I><TT><I>$firstVar</I></TT><I>three places to the right and assign the new value to </I><TT><I>$secondVar</I></TT><I>.<BR>Print the </I><TT><I>$secondVar</I></TT><I>variable.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 129;$secondVar = $firstVar >> 3;print("$secondVar\n");</PRE></BLOCKQUOTE><P>The program produces the following output:<BLOCKQUOTE><PRE>16</PRE></BLOCKQUOTE><P>SiNCe the bit value of 16 is 00010000, you can tell that the rightmostbit has disappeared.<P>Here's a quick example using the <TT><<</TT>operator. We'll multiply 128 by 8.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign a value of 128 to the </I><TT><I>$firstVar</I></TT><I>variable.<BR>Shift the bits inside </I><TT><I>$firstVar</I></TT><I>two places to the left and assign the new value to </I><TT><I>$secondVar</I></TT><I>.<BR>Print the </I><TT><I>$secondVar</I></TT><I>variable.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 128;$secondVar = $firstVar << 3;print $secondVar;</PRE></BLOCKQUOTE><P>The program produces the following output:<BLOCKQUOTE><PRE>1024</PRE></BLOCKQUOTE><P>The value of 1024 is beyond the bounds of the 8 bits that theother examples used. This was done to show you that the numberof bits available for your use is not limited to one byte. Youare really limited by however many bytes Perl uses for one scalarvariable-probably 4. You'll need to read the Perl documentationthat came with the interpreter to determine how many bytes yourscalar variables use.<H2><A NAME="TheNumericRelationalOperators"><FONT SIZE=5 COLOR=#FF0000>The Numeric Relational Operators</FONT></A></H2><P>The <I>numeric relational </I>operators, listed in Table 4.9,are used to test the relationship between two operands. You cansee if one operand is equal to another, if one operand is greaterthan another, or if one operator is less than another.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>It is important to realize that the equality operator is a pair of equal signs and not just one. Quite a few bugs are introduced into programs because people forget this rule and use a single equal sign when testing conditions.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P><CENTER><B>Table 4.9 The Numeric Relational Operators</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=153><CENTER><I>Operator</I></CENTER></TD><TD WIDTH=437><I>Description</I></TD></TR><TR><TD COLSPAN=2 WIDTH=590><B>The Equality Operators</B></TD></TR><TR><TD WIDTH=153><CENTER><TT>op1 == op2</TT></CENTER></TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is equal to <TT>op2</TT>. For example, 6 == 6 is true.</TD></TR><TR><TD WIDTH=153><CENTER><TT>op1 != op2</TT></CENTER></TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is not equal to <TT>op2</TT>. For example, 6 != 7 is true.</TD></TR><TR><TD COLSPAN=2 WIDTH=590><B>The Comparison Operators</B></TD></TR><TR><TD WIDTH=153><CENTER><TT>op1 < op2</TT></CENTER></TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is less than <TT>op2</TT>. For example, 6 < 7 is true.</TD></TR><TR><TD WIDTH=153><CENTER><TT>Op1 <= op2</TT></CENTER></TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is less than or equal to <TT>op2</TT>. For example, 7 <= 7 is true.</TD></TR><TR><TD WIDTH=153><CENTER><TT>op1 > op2</TT></CENTER></TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is greater than <TT>op2</TT>. For example, 6 > 5 is true.</TD></TR><TR><TD WIDTH=153><CENTER><TT>op1 >= op2</TT></CENTER>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -