⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch03_13.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Bitwise Operators (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Bitwise Operators"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch03_12.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch03_01.htm">Chapter 3: Unary and Binary Operators</a></td><td align="right" valign="top" width="172"><a href="ch03_14.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">3.13. Bitwise Operators</h2><p><a name="INDEX-953"></a><a name="INDEX-954"></a><a name="INDEX-955"></a><a name="INDEX-956"></a><a name="INDEX-957"></a><a name="INDEX-958"></a><a name="INDEX-959"></a><a name="INDEX-960"></a><a name="INDEX-961"></a>Like C, Perl has bitwise AND, OR, and XOR (exclusive OR) operators:<tt class="literal">&amp;</tt>, <tt class="literal">|</tt>, and <tt class="literal">^</tt>.  You'll have noticed from your painstakingexamination of the table at the start of this chapter that bitwise ANDhas a higher precedence than the others, but we've cheated and combinedthem in this discussion.</p><p><a name="INDEX-962"></a><a name="INDEX-963"></a>These operators work differently on numeric values than they do onstrings. (This is one of the few places where Perl cares about thedifference.) If either operand is a number (or has been used as anumber), both operands are converted to integers, and thebitwise operation is performed between the two integers.  Theseintegers are guaranteed to be at least 32 bits long, but can be 64 bitson some machines.  The point is that there's an arbitrary limit imposedby the machine's architecture.</p><p>If both operands are strings (and have not been used as numbers sincethey were set), the operators do bitwise operations between correspondingbits from the two strings.  In this case, there's no arbitrary limit,since strings aren't arbitrarily limited in size.  If one string islonger than the other, the shorter string is considered to have asufficient number of 0 bits on the end to make up the difference.</p><p>For example, if you AND together two strings:<blockquote><pre class="programlisting">"123.45" &amp; "234.56"</pre></blockquote>you get another string:<blockquote><pre class="programlisting">"020.44"</pre></blockquote>But if you AND together a string and a number:<blockquote><pre class="programlisting">"123.45" &amp; 234.56</pre></blockquote>The string is first converted to a number, giving:<blockquote><pre class="programlisting">123.45 &amp; 234.56</pre></blockquote>The numbers are then converted to integers:<blockquote><pre class="programlisting">123 &amp; 234</pre></blockquote>which evaluates to 106.  Note that all bit strings are true (unlessthey result in the string "<tt class="literal">0</tt>").  This means if youwant to see whether any byte came out to nonzero, instead of writingthis:<blockquote><pre class="programlisting">if ( "fred" &amp; "\1\2\3\4" ) { ... }</pre></blockquote>you need to write this:<blockquote><pre class="programlisting">if ( ("fred" &amp; "\1\2\3\4") =~ /[^\0]/ ) { ... }</pre></blockquote></p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch03_12.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch03_14.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">3.12. Equality Operators</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">3.14. C-Style Logical (Short-Circuit) Operators</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -