📄 ch4.htm
字号:
<TR><TD WIDTH=67><CENTER>0</CENTER></TD><TD WIDTH=70><CENTER>1</CENTER>
</TD><TD WIDTH=140><CENTER>0</CENTER></TD></TR>
<TR><TD WIDTH=67><CENTER>1</CENTER></TD><TD WIDTH=70><CENTER>1</CENTER>
</TD><TD WIDTH=140><CENTER>1</CENTER></TD></TR>
</TABLE>
</CENTER>
<P>
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>If the value of </I><TT><I>$firstVar</I></TT><I>
is 10 AND the value of </I><TT><I>$secondVar</I></TT><I>
is 9, then print the error message.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
if ($firstVar == 10 && $secondVar == 9) {
print("Error!");
};
</PRE>
</BLOCKQUOTE>
<P>
If either of the two conditions is false or iNCorrect, then the
print command is bypassed.
<H3><A NAME="ExampleThequotORquotOperator">
Example: The "OR" Operator (||)</A></H3>
<P>
The || operator is used to determine whether either of the conditions
is true. Table 4.6 shows the results of using the || operator
on the four sets of true/false values.<BR>
<P>
<CENTER><B>Table 4.6 The || Result Table</B></CENTER>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD WIDTH=73><CENTER><I>0p1 </I></CENTER></TD><TD WIDTH=94><CENTER><I>0p2 </I></CENTER>
</TD><TD WIDTH=104><CENTER><I>0p1 || 0p2</I></CENTER></TD></TR>
<TR><TD WIDTH=73><CENTER>0</CENTER></TD><TD WIDTH=94><CENTER>0</CENTER>
</TD><TD WIDTH=104><CENTER>0</CENTER></TD></TR>
<TR><TD WIDTH=73><CENTER>1</CENTER></TD><TD WIDTH=94><CENTER>0</CENTER>
</TD><TD WIDTH=104><CENTER>1</CENTER></TD></TR>
<TR><TD WIDTH=73><CENTER>0</CENTER></TD><TD WIDTH=94><CENTER>1</CENTER>
</TD><TD WIDTH=104><CENTER>1</CENTER></TD></TR>
<TR><TD WIDTH=73><CENTER>1</CENTER></TD><TD WIDTH=94><CENTER>1</CENTER>
</TD><TD WIDTH=104><CENTER>1</CENTER></TD></TR>
</TABLE>
</CENTER>
<P>
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>If the value of </I><TT><I>$firstVar</I></TT><I>
is 9 OR the value of </I><TT><I>$firstVar</I></TT><I>
is 10, then print the error message.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
if ($firstVar == 9 </FONT><FONT SIZE=2 FACE="Palatino">||</FONT><FONT SIZE=2 FACE="Courier"> $firstVar == 10) {
print("Error!");
</PRE>
</BLOCKQUOTE>
<P>
If either of the two conditions is true, then the print command
is run.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Caution</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
If the first operand of the || operator evaluates to true, the second operand will not be evaluated. This could be a source of bugs if you are not careful. For instaNCe, in the following code fragment:</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>if ($firstVar++ || $secondVar++) { print("\n"); }</TT>
</BLOCKQUOTE>
<BLOCKQUOTE>
variable <TT>$secondVar</TT> will not be iNCremented if <TT>$firstVar++</TT> evaluates to true.
</BLOCKQUOTE>
</TD></TR>
</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 to
convert true values to false and false values to true. In other
words, it inverts a value. Perl considers any non-zero value to
be true-even string values. Table 4.7 shows the results of using
the <TT>!</TT> operator on true and
false 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" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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>and
assign 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 similar
to 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 signify
the true/false status of 8 flags because each bit can be used
as a boolean variable that can hold one of two values: true or
false. A <I>flag</I> variable is typically used to indicate the
status of something. For instaNCe, computer files can be marked
as read-only. So you might have a <TT>$fReadOnly</TT>
variable whose job would be to hold the read-only status of a
file. This variable is called a flag variable because when <TT>$fReadOnly</TT>
has a true value, it's equivalent to a football referee throwing
a flag. The variable says, "Whoa! Don't modify this file."
<P>
When you have more than one flag variable, it might be more efficient
to use a single variable to indicate the value of more than one
flag. 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 than
one flag in a single variable is to define the meaning of the
bits that you'd like to use. Figure 4.1 shows an example of 8
bits that could be used to control the attributes of text on a
display.
<P>
<A HREF="f4-1.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/f4-1.gif"><B>Figure 4.1 : </B><I>The bit definition of a text attribute control
variable</I>.</A>
<P>
If you assume that <TT>$textAttr</TT>
is used to control the text attributes, then you could set the
italic 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 turned
on corresponds to the italic position in <TT>$textAttr</TT>.
<P>
Now let's set both the italic and underline attributes on at the
same time. The underline value is 16, which has a bit pattern
of 00010000. You already know the value for italic is 128. So
we call on the <TT>OR</TT> operator
to 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 do
this 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 will
see that <TT>$textAttr</TT> gets assigned
a value of 144 (or 10010000 as a bit pattern). This will set both
italic and underline attributes on.
<P>
The next step might be to turn the italic attribute off. This
is 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 the
bits in the operand left or right a given number of times. They
come in quite handy when you need to divide or multiply integer
values.
<P>
This example will divide by 4 using the <TT>>></TT>
operator.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 after
the shift operation. First, <TT>$firstVar</TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -