?? apc.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Teach Yourself C++ in 21 Days</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1></H1>
<H2 ALIGN="CENTER"><A HREF="apb.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/apb.htm"><IMG SRC="../buttons/BLANPREV.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANPREV.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="javascript:if(confirm('http://www.mcp.com/sams \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.mcp.com/sams'" tppabs="http://www.mcp.com/sams"><IMG
SRC="../buttons/BLANHOME.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"
BORDER="0"></A><A HREF="../index.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/index.htm"><IMG SRC="../buttons/BLANTOC.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANTOC.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="apd.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/apd.htm"><IMG SRC="../buttons/BLANNEXT.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANNEXT.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><BR>
<BR>
<BR>
<A NAME="Heading1"></A><FONT COLOR="#000077">Appendix C<BR>
<BR>
Binary and Hexadecimal</FONT></H2>
<H2 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H2>
<P>You learned the fundamentals of arithmetic so long ago, it is hard to imagine
what it would be like without that knowledge. When you look at the number 145 you
instantly see "one hundred and forty-five" without much reflection.</P>
<P>Understanding binary and hexadecimal requires that you re-examine the number 145
and see it not as a number, but as a code for a number.</P>
<P>Start small: Examine the relationship between the number three and "3."
The numeral 3 is a squiggle on a piece of paper; the number three is an idea. The
numeral is used to represent the number.</P>
<P>The distinction can be made clear by realizing that three, 3, |||, III, and ***
all can be used to represent the same idea of three.</P>
<P>In base 10 (decimal) math you use the numerals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 to
represent all numbers. How is the number ten represented?</P>
<P>One can imagine that we would have evolved a strategy of using the letter A to
represent ten; or we might have used IIIIIIIIII to represent that idea. The Romans
used X. The Arabic system, which we use, makes use of position in conjunction with
numerals to represent values. The first (right-most) column is used for "ones,"
and the next column is used for tens. Thus, the number fifteen is represented as
15 (read "one, five"); that is, 1 ten and 5 ones.</P>
<P>Certain rules emerge, from which some generalizations can be made:
<DL>
<DD><B>1.</B> Base 10 uses the digits 0-9.<BR>
<BR>
<B>2.</B> The columns are powers of ten: 1s, 10s, 100s, and so on.<BR>
<B><BR>
3.</B> If the third column is 100, the largest number you can make with two columns
is 99. More generally, with n columns you can represent 0 to (10<SUP>n</SUP>-1).
Thus, with 3 columns you can represent 0 to (10<SUP>3</SUP>-1) or 0-999.
</DL>
<H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">Other Bases</FONT></H3>
<P>It is not a coincidence that we use base 10; we have 10 fingers. One can imagine
a different base, however. Using the rules found in base 10, you can describe base
8:
<DL>
<DD><B>1.</B> The digits used in base 8 are 0-7.<BR>
<B><BR>
2.</B> The columns are powers of 8: 1s, 8s, 64, and so on.<BR>
<B>3.</B> With n columns you can represent 0 to 8<SUP>n</SUP>-1.
</DL>
<P>To distinguish numbers written in each base, write the base as a subscript next
to the number. The number fifteen in base 10 would be written as 15<SUB>10</SUB>
and read as "one, five, base ten."</P>
<P>Thus, to represent the number 15<SUB>10</SUB> in base 8 you would write 17<SUB>8</SUB>.
This is read "one, seven, base eight." Note that it can also be read "fifteen"
as that is the number it continues to represent.</P>
<P>Why 17? The 1 means 1 eight, and the 7 means 7 ones. One eight plus seven ones
equals fifteen. Consider fifteen asterisks:</P>
<PRE><FONT COLOR="#0066FF">
***** *****
</FONT></PRE>
<PRE><FONT COLOR="#0066FF">
*****
</FONT></PRE>
<P>The natural tendency is to make two groups, a group of ten asterisks and another
of five. This would be represented in decimal as 15 (1 ten and 5 ones). You can also
group the asterisks as</P>
<PRE><FONT COLOR="#0066FF">
**** *******
</FONT></PRE>
<PRE><FONT COLOR="#0066FF">
****
</FONT></PRE>
<P>That is, eight asterisks and seven. That would be represented in base eight as
17<SUB>8</SUB>. That is, one eight and seven ones.
<H3 ALIGN="CENTER"><A NAME="Heading4"></A><FONT COLOR="#000077">Around the Bases</FONT></H3>
<P>You can represent the number fifteen in base ten as 15, in base nine as 16<SUB>9</SUB>,
in base 8 as 17<SUB>8</SUB>, in base 7 as 21<SUB>7</SUB>. Why 21<SUB>7</SUB>? In
base 7 there is no numeral 8. In order to represent fifteen, you will need two sevens
and one 1.</P>
<P>How do you generalize the process? To convert a base ten number to base 7, think
about the columns: in base 7 they are ones, sevens, forty-nines, three-hundred forty-threes,
and so on. Why these columns? They represent 7<SUP>0</SUP>, 7<SUP>1</SUP>, 7<SUP>2</SUP>,
7<SUP>4</SUP> and so forth. Create a table for yourself:
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">4</TD>
<TD ALIGN="LEFT">3</TD>
<TD ALIGN="LEFT">2</TD>
<TD ALIGN="LEFT">1</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">7<SUP>3</SUP></TD>
<TD ALIGN="LEFT">7<SUP>2</SUP></TD>
<TD ALIGN="LEFT">7<SUP>1</SUP></TD>
<TD ALIGN="LEFT">7<SUP>0</SUP></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>343
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>49
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>7
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>1
</BLOCKQUOTE>
<P>
</TD>
</TR>
</TABLE>
The first row represents the column number. The second row represents the power of
7. The third row represents the decimal value of each number in that row.</P>
<P>To convert from a decimal value to base 7, here is the procedure: Examine the
number and decide which column to use first. If the number is 200, for example, you
know that column 4 (343) is 0, and you don't have to worry about it.</P>
<P>To find out how many 49s there are, divide 200 by 49. The answer is 4, so put
4 in column 3 and examine the remainder: 4. There are no 7s in 4, so put a zero in
the sevens column. There are 4 ones in 4, so put a 4 in the 1s column. The answer
is 404<SUB>7</SUB>.</P>
<P>To convert the number 968 to base 6:
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">5</TD>
<TD ALIGN="LEFT">4</TD>
<TD ALIGN="LEFT">3</TD>
<TD ALIGN="LEFT">2</TD>
<TD ALIGN="LEFT">1</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">6<SUP>4</SUP></TD>
<TD ALIGN="LEFT">6<SUP>3</SUP></TD>
<TD ALIGN="LEFT">6<SUP>2</SUP></TD>
<TD ALIGN="LEFT">6<SUP>1</SUP></TD>
<TD ALIGN="LEFT">6<SUP>0</SUP></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>1296
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>216
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>36
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>6
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>1
</BLOCKQUOTE>
<P>
</TD>
</TR>
</TABLE>
There are no 1296s in 968, so column 5 has 0. Dividing 968 by 216 yields 4 with a
remainder of 104. Column 4 is 4. Dividing 104 by 36 yields 2 with a remainder of
32. Column 3 is 2. Dividing 32 by 6 yields 5 with a remainder of 2. The answer therefore
is 4252<SUB>6</SUB>.
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">5</TD>
<TD ALIGN="LEFT">4</TD>
<TD ALIGN="LEFT">3</TD>
<TD ALIGN="LEFT">2</TD>
<TD ALIGN="LEFT">1</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">6<SUP>4</SUP></TD>
<TD ALIGN="LEFT">6<SUP>3</SUP></TD>
<TD ALIGN="LEFT">6<SUP>2</SUP></TD>
<TD ALIGN="LEFT">6<SUP>1</SUP></TD>
<TD ALIGN="LEFT">6<SUP>0</SUP></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">1296</TD>
<TD ALIGN="LEFT">216</TD>
<TD ALIGN="LEFT">36</TD>
<TD ALIGN="LEFT">6</TD>
<TD ALIGN="LEFT">1</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>0
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>4
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>2
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>5
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>2
</BLOCKQUOTE>
<P>
</TD>
</TR>
</TABLE>
There is a shortcut when converting from one base to another base (such as 6) to
base 10. You can multiply:
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">4 * 216</TD>
<TD ALIGN="LEFT">=</TD>
<TD ALIGN="LEFT">864</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">2 * 36</TD>
<TD ALIGN="LEFT">=</TD>
<TD ALIGN="LEFT">72</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">5 * 6</TD>
<TD ALIGN="LEFT">=</TD>
<TD ALIGN="LEFT">30</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">2 * 1</TD>
<TD ALIGN="LEFT">=</TD>
<TD ALIGN="LEFT">2</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>968
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT"></TD>
</TR>
</TABLE>
<CENTER>
<H4><A NAME="Heading5"></A><FONT COLOR="#000077">Binary</FONT></H4>
</CENTER>
<P>Base 2 is the ultimate extension of this idea. There are only two digits: 0 and
1. The columns are:
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Col:</TD>
<TD ALIGN="LEFT">8</TD>
<TD ALIGN="LEFT">7</TD>
<TD ALIGN="LEFT">6</TD>
<TD ALIGN="LEFT">5</TD>
<TD ALIGN="LEFT">4</TD>
<TD ALIGN="LEFT">3</TD>
<TD ALIGN="LEFT">2</TD>
<TD ALIGN="LEFT">1</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Power:</TD>
<TD ALIGN="LEFT">2<SUP>7</SUP></TD>
<TD ALIGN="LEFT">2<SUP>6</SUP></TD>
<TD ALIGN="LEFT">2<SUP>5</SUP></TD>
<TD ALIGN="LEFT">2<SUP>4</SUP></TD>
<TD ALIGN="LEFT">2<SUP>3</SUP></TD>
<TD ALIGN="LEFT">2<SUP>2</SUP></TD>
<TD ALIGN="LEFT">2<SUP>1</SUP></TD>
<TD ALIGN="LEFT">2<SUP>0</SUP></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>Value:
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>128
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>64
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>32
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>16
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>8
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>4
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>2
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>1
</BLOCKQUOTE>
<P>
</TD>
</TR>
</TABLE>
To convert the number 88 to base 2, you follow the same procedure: There are no 128s,
so column 8 is 0.</P>
<P>There is one 64 in 88, so column 7 is 1 and 24 is the remainder. There are no
32s in 24 so column 6 is 0.</P>
<P>There is one 16 in 24 so column 5 is 1. The remainder is 8. There is one 8 in
8, and so column 4 is 1. There is no remainder, so the rest of the columns are 0.
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>0
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>1
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>0
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>1
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>1
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>0
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>0
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>0
</BLOCKQUOTE>
<P>
</TD>
</TR>
</TABLE>
To test this answer, convert it back:</P>
<PRE><FONT COLOR="#0066FF">
1 * 64 = 64
0 * 32 = 0
1 * 16 = 16
1 * 8 = 8
0 * 4 = 0
0 * 2 = 0
0 * 1 = 0 88
</FONT></PRE>
<CENTER>
<H4><A NAME="Heading6"></A><FONT COLOR="#000077">Why Base 2?</FONT></H4>
</CENTER>
<P>The power of base 2 is that it corresponds so cleanly to what a computer needs
to represent. Computers do not really know anything at all about letters, numerals,
instructions, or programs. At their core they are just circuitry, and at a given
juncture there either is a lot of power or there is very little.</P>
<P>To keep the logic clean, engineers do not treat this as a relative scale (a little
power, some power, more power, lots of power, tons of power), but rather as a binary
scale ("enough power" or "not enough power"). Rather than saying
"enough" or "not enough," they simplify it to "yes"
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -