📄 chap13.html
字号:
<P> spock = tempSpock;</P>
<P> }</P>
<P> }</P>
<P>}</P>
</FONT><FONT SIZE="2"><P> </P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>The bytecodes generated by <TT>javac</TT> for <FONT FACE="Courier New">incrementLogically()</FONT> are:</P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT SIZE="2"><P></FONT><FONT FACE="Courier New"> 0 iconst_0 // Push int constant 0.
<P> 1 istore_0 // Pop to local variable 0: int spock = 0;</P>
<P> 2 iload_0 // Push local variable 0 (spock).</P>
<P> 3 istore_1 // Pop to local variable 1: int tempSpock = spock;</P>
<P> 4 iconst_0 // Push int constant 0.</P>
<P> 5 istore_2 // Pop to local variable 2: int i = 0;</P>
<P> 6 goto 35 // Jump unconditionally ()</P>
<P> 9 iconst_1 // Push int constant 1.</P>
<P>10 iload_2 // Push local variable 2 (i).</P>
<P>11 ishl // Arithmetic shift left top int (i) by next</P>
<P> // to top int (1).</P>
<P>12 istore_3 // Pop to local variable 3: int mask = i <FONT FACE="Courier New"> 0x1;</P>
<P>13 iload_1 // Push local variable 1 (tempSpock).</P>
<P>14 iload_3 // Push local variable 3 (mask).</P>
<P>15 iand // Bitwise AND top two ints: (spock & mask)</P>
<P>16 ifne 26 // Jump if top of stack is not equal to zero:</P>
<P> // if ((spock & mask) == 0) {</P>
<P>19 iload_1 // Push local variable 1 (tempSpock).</P>
<P>20 iload_3 // Push local variable 3 (mask).</P>
<P>21 ior // Bitwise OR top two ints (tempSpock | mask)</P>
<P>22 istore_1 // Pop to local variable 1: tempSpock |= mask;</P>
<P>23 goto 41 // Jump unconditionally (to just after</P>
<P> // inner for): break;</P>
<P>26 iload_1 // Push local variable 1 (tempSpock).</P>
<P>27 iload_3 // Push local variable 3 (mask).</P>
<P>28 iconst_m1 // Push -1.</P>
<P>29 ixor // Bitwise EXCLUSIVE-OR top two ints: ~mask</P>
<P>30 iand // Bitwise AND top two ints: tempSpock & (~mask)</P>
<P>31 istore_1 // Pop to local variable 1: tempSpock &= ~mask;</P>
<P>32 iinc 2 1 // Increment local variable 2 by 1: ++i</P>
<P>35 iload_2 // Push local variable 2 (i).</P>
<P>36 bipush 32 // Push integer constant 32.</P>
<P>38 if_icmplt 9 // Jump (to top of inner for) if "next to top"</P>
<P> // integer is less than "top" integer: i < 32</P>
<P>41 iload_1 // Push local variable 1 (tempSpock)</P>
<P>42 istore_0 // Pop to local variable 0: spock = tempSpock;</P>
<P>43 goto 2 // Jump unconditionally (to top of outer for).</P>
</FONT><FONT SIZE="2"><P> </P></FONT><FONT FACE="Courier New">end</PRE></FONT></P></PRE>
<P>The <FONT FACE="Courier New">incrementLogically()</FONT> method repeatedly increments an <FONT FACE="Courier New">int</FONT> without using the <FONT FACE="Courier New">+</FONT> or <FONT FACE="Courier New">++</FONT> operators. It uses only the logical operators: <FONT FACE="Courier New">&</FONT>, <FONT FACE="Courier New">|</FONT>, and <FONT FACE="Courier New">~</FONT>. An increment is accomplished by searching through the bits of the current <FONT FACE="Courier New">int</FONT>, starting with the lowest order bit, and turning ones to zeros. As soon as a one is encountered in the current <FONT FACE="Courier New">int</FONT>, it is changed to zero and the search stops. The resultant <FONT FACE="Courier New">int</FONT> now represents the old <FONT FACE="Courier New">int</FONT> incremented by one. The process is started again on the new <FONT FACE="Courier New">int</FONT>. Each incremented number is stored in the <FONT FACE="Courier New">spock</FONT> variable. <FONT FACE="Courier New">spock</FONT> is local variable zero in the compiled bytecodes, so you can watch local variable zero count 0, 1, 2, 3, and so on.</P>
<P>To drive the <I>Logical Results</I> simulation, use the Step, Reset, Run, and Stop buttons. Each time you press the Step button, the simulator will execute the instruction pointed to by the pc register. If you press the Run button, the simulation will continue with no further coaxing on your part until you press the Stop button. To start the simulation over, press the Reset button. For each step of the simulation, a panel at the bottom of the applet contains an explanation of what the next instruction will do. Happy clicking.</P>
<P><IMG SRC="fig13-1.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/images/fig13-1.gif" ALT="Figure 13-1"></P></PRE>
<H3><EM><P>On the CD-ROM</P>
</EM></H3><P>The CD-ROM contains the source code examples from this chapter in the <FONT FACE="Courier New">opcodes</FONT> directory. The <I>Logical Results</I> applet is in a web page on the CD-ROM in file <FONT FACE="Courier New">applets/LogicalResults.html</FONT>. The source code for this applet is found alongside its class files, in the <FONT FACE="Courier New">applets/JVMSimulators</FONT> and <FONT FACE="Courier New">applets/JVMSimulators/COM/artima/jvmsim</FONT> directories.</P>
<H3><EM><P>The Resources Page</P></PRE>
</EM></H3><P>For more information about the material presented in this chapter, visit the resources page: <FONT FACE="Courier New"><A HREF="http://www.artima.com/insidejvm/logic.html" tppabs="http://www.artima.com/insidejvm/logic.html">http://www.artima.com/insidejvm/logic.html</A></FONT>.</P>
<TABLE BORDER="0" WIDTH="100%">
<TR><TD><A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" target="bottom"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Orders"></A>
<IMG SRC="order_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/order_text.gif" WIDTH="103" HEIGHT="41" ALT="Orders"></TD>
<TD ALIGN="RIGHT"><A HREF="chap12.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap12.html"><IMG SRC="backward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/backward.gif" BORDER="0" ALT="Backward" WIDTH="32" HEIGHT="32"></A> <A HREF="chap14.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap14.html"><IMG SRC="forward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/forward.gif" BORDER="0" ALT="Forward" WIDTH="32" HEIGHT="32"></A></TD></TR>
<TR><TD COLSPAN="2"><A HREF="mailto:computing@mcgraw-hill.com"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Comments"></A>
<IMG SRC="comment_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/comment_text.gif" WIDTH="73" HEIGHT="39" ALT="Comments"></TD></TR>
</TABLE>
<HR>
<P ALIGN=CENTER> <A HREF="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" TARGET="_top">COMPUTING
MCGRAW-HILL</A> | <A HREF="http://www.pbg.mcgraw-hill.com/betabooks/betabooks-home.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/betabooks-home.html" TARGET="_top">Beta Books</A>
| <A HREF="http://www.pbg.mcgraw-hill.com/computing/contact.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/contact.html" TARGET="_top">Contact Us</A>
| <A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" TARGET="_top">Order Information</A>
| <A HREF="http://mcgraw-hill.inforonics.com/compsearch.shtml" tppabs="http://mcgraw-hill.inforonics.com/compsearch.shtml" TARGET="_top">Online Catalog</A></P>
<P ALIGN=CENTER><FONT SIZE="-1"><A HREF="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" TARGET="_top">Computing McGraw-Hill</A> is an imprint of the <A HREF="http://www.pbg.mcgraw-hill.com/pbg-home.html" tppabs="http://www.pbg.mcgraw-hill.com/pbg-home.html" TARGET="_top">McGraw-Hill Professional Book Group</A>.</FONT></P>
<!-- begin footer -->
<HR>
<A HREF="http://www.mcgraw-hill.com/" tppabs="http://www.mcgraw-hill.com/" TARGET="_top"><IMG SRC="division-white.gif" tppabs="http://www.pbg.mcgraw-hill.com/images/division-white.gif" WIDTH="350" HEIGHT="44" ALT="A Division of the McGraw-Hill Companies" BORDER="0"></A><BR>
<FONT SIZE="-2">Copyright © 1997 <A HREF="http://www.mcgraw-hill.com/" tppabs="http://www.mcgraw-hill.com/" TARGET="_top">
The McGraw-Hill Companies</A>. All rights reserved. Any use is subject to the
<A HREF="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" tppabs="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" TARGET="_top">
Terms of Use</A>; the corporation also has a comprehensive <A HREF="http://www.mcgraw-hill.com/corporate/news_info/privacy.html" tppabs="http://www.mcgraw-hill.com/corporate/news_info/privacy.html" TARGET="_top">
Privacy Policy</A> governing information we may collect from our customers.</FONT>
<!-- end footer -->
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -