📄 chapter 3 numbers, characters and strings -- valvano.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0056)http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm -->
<HTML><HEAD><TITLE>Chapter 3: Numbers, Characters and Strings -- Valvano</TITLE>
<META http-equiv=content-type content=text/html;charset=iso-8859-1>
<META content="MSHTML 5.50.3825.1300" name=GENERATOR>
<META
content="Power HD:Applications:Microsoft Office 98:Templates:Web Pages:Blank Web Page"
name=Template></HEAD>
<BODY vLink=#800080 link=#0000ff>
<P><!--Developing Embedded Software in C using ICC11/ICC12/Hiware by Jonathan W. Valvano--><B><FONT
face=Helvetica,Arial size=4>Chapter 3: Numbers, Characters and
Strings</FONT></B><FONT face=Helvetica,Arial size=4> </FONT></P>
<P><B><I><FONT face=Helvetica,Arial>What's in Chapter 3?</FONT></I></B></P>
<DIR>
<P><A href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#BINARY">How
are numbers represented on the computer</A> <FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#BM8BITUNSIGNED">8-bit
unsigned numbers</A> <FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#BM8BITSIGNED">8-bit
signed numbers</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#BM16BITUNSIGNED">16-bit
unsigned numbers</A> <FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#BM16BITSIGNED">16-bit
signed numbers</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#ENDIAN">Big and
little endian </A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#BOOLEAN">Boolean
(true/false)</A> <FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#DECIMAL">Decimal
numbers</A> <FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#HEX">Hexadecimal
numbers</A> <FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#OCTAL">Octal
numbers</A> <FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#CHARACTER">Characters</A>
<FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#STRING">Strings</A><FONT
face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#ESCAPE">Escape
sequences</A> </P></DIR>
<P>This chapter defines the various data types supported by the compiler. Since
the objective of most computer systems is to process data, it is important to
understand how data is stored and interpreted by the software. We define a
<I>literal</I> as the direct specification of the number, character, or string.
E.g.,</P>
<DIR>
<DIR>
<P><CODE>100 'a' "Hello World"</CODE></P></DIR></DIR>
<P>are examples of a number literal, a character literal and a string literal
respectively. We will discuss the way data are stored on the computer as well as
the C syntax for creating the literals. The Imagecraft and Hiware compilers
recognize three types of literals (<I>numeric</I>, <I>character</I>,
<I>string</I>). Numbers can be written in three bases (<I><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#DECIMAL">decimal</A></I>,
<I><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#OCTAL">octal</A></I>,
and <I><A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#HEX">hexadecimal</A></I>).
Although the programmer can choose to specify numbers in these three bases, once
loaded into the computer, the all numbers are stored and processed as unsigned
or signed binary. Although C does not support the binary literals, if you wanted
to specify a binary number, you should have no trouble using either the octal or
hexadecimal format.</P>
<P><B><I><FONT face=Helvetica,Arial><A name=BINARY></A>Binary
representation</FONT></I></B></P>
<P>Numbers are stored on the computer in binary form. In other words,
information is encoded as a sequence of 1’s and 0’s. <I>Precision</I> is the
number of distinct or different values. We express precision in alternatives,
decimal digits, bytes, or binary bits. We use the expression 4<SUP>1/2</SUP>
decimal digits to mean about 20,000 alternatives, and the expression
4<SUP>3/4</SUP> decimal digits to mean more than 20,000 alternatives but less
than 100,000 alternatives. The following table illustrates the various
representations of precision. </P>
<P>
<TABLE cellSpacing=0 width=398 border=0>
<TBODY>
<TR>
<TD vAlign=top width="22%"><B>binary bits</B></TD>
<TD vAlign=top width="17%"><B>bytes</B></TD>
<TD vAlign=top width="32%"><B>alternatives</B></TD>
<TD vAlign=top width="29%"><B>decimal digits</B></TD></TR>
<TR>
<TD vAlign=top width="22%">8</TD>
<TD vAlign=top width="17%">1</TD>
<TD vAlign=top width="32%">256</TD>
<TD vAlign=top width="29%">2 <SUP>1/2</SUP></TD></TR>
<TR>
<TD vAlign=top width="22%">10</TD>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="32%">1024</TD>
<TD vAlign=top width="29%">3</TD></TR>
<TR>
<TD vAlign=top width="22%">12</TD>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="32%">4096</TD>
<TD vAlign=top width="29%">3 <SUP>3/4</SUP></TD></TR>
<TR>
<TD vAlign=top width="22%">16</TD>
<TD vAlign=top width="17%">2</TD>
<TD vAlign=top width="32%">65,536</TD>
<TD vAlign=top width="29%">4 <SUP>3/4</SUP></TD></TR>
<TR>
<TD vAlign=top width="22%">20</TD>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="32%">1,048,576</TD>
<TD vAlign=top width="29%">5</TD></TR>
<TR>
<TD vAlign=top width="22%">24</TD>
<TD vAlign=top width="17%">3</TD>
<TD vAlign=top width="32%">16,777,216</TD>
<TD vAlign=top width="29%">7 <SUP>1/2</SUP></TD></TR>
<TR>
<TD vAlign=top width="22%">30</TD>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="32%">1,073,741,824</TD>
<TD vAlign=top width="29%">9</TD></TR>
<TR>
<TD vAlign=top width="22%">32</TD>
<TD vAlign=top width="17%">4</TD>
<TD vAlign=top width="32%">4,294,967,296</TD>
<TD vAlign=top width="29%">9 <SUP>3/4</SUP></TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 3-1. Relationships between various representations of
precision.</ADDRESS>
<P> </P>
<DIR>
<P>
<ADDRESS>Observation: A good rule of thumb to remember is 2<SUP>10•n </SUP>is
about 10<SUP>3•n</SUP>. </ADDRESS>
<DIR>
<P> </P></DIR></DIR>
<P>For large numbers we use abbreviations, as shown in the following table. For
example, 16K means 16*1024 which equals 16384. Computer engineers use the same
symbols as other scientists, but with slightly different values. </P>
<P> </P>
<P>
<TABLE cellSpacing=0 width=487 border=0>
<TBODY>
<TR>
<TD vAlign=top width="21%">abbreviation</TD>
<TD vAlign=top width="18%">pronunciation</TD>
<TD vAlign=top width="39%">Computer Engineering Value</TD>
<TD vAlign=top width="21%">Scientific Value</TD></TR>
<TR>
<TD vAlign=top width="21%">K</TD>
<TD vAlign=top width="18%">"kay"</TD>
<TD vAlign=top width="39%">2<SUP>10</SUP> 1024</TD>
<TD vAlign=top width="21%">10<SUP>3</SUP> </TD></TR>
<TR>
<TD vAlign=top width="21%">M</TD>
<TD vAlign=top width="18%">"meg"</TD>
<TD vAlign=top width="39%">2<SUP>20</SUP> 1,048,576</TD>
<TD vAlign=top width="21%">10<SUP>6</SUP> </TD></TR>
<TR>
<TD vAlign=top width="21%">G</TD>
<TD vAlign=top width="18%">"gig"</TD>
<TD vAlign=top width="39%">2<SUP>30</SUP> 1,073,741,824</TD>
<TD vAlign=top width="21%">10<SUP>9</SUP> </TD></TR>
<TR>
<TD vAlign=top width="21%">T</TD>
<TD vAlign=top width="18%">"tera"</TD>
<TD vAlign=top width="39%">2<SUP>40</SUP> 1,099,511,627,776</TD>
<TD vAlign=top width="21%">10<SUP>12</SUP> </TD></TR>
<TR>
<TD vAlign=top width="21%">P</TD>
<TD vAlign=top width="18%">"peta"</TD>
<TD vAlign=top width="39%">2<SUP>50</SUP> 1,125,899,906,843,624</TD>
<TD vAlign=top width="21%">10<SUP>15</SUP> </TD></TR>
<TR>
<TD vAlign=top width="21%">E</TD>
<TD vAlign=top width="18%">"exa"</TD>
<TD vAlign=top width="39%">2<SUP>60</SUP> 1,152,921,504,606,846,976</TD>
<TD vAlign=top width="21%">10<SUP>18</SUP> </TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 3-2. Common abbreviations for large numbers.</ADDRESS>
<P> </P>
<P><B><I><FONT face=Helvetica,Arial><A name=BM8BITUNSIGNED></A>8-bit unsigned
numbers</FONT></I></B></P>
<P>A byte contains 8 bits</P>
<P> </P>
<DIR>
<DIR>
<P><IMG height=32
src="Chapter 3 Numbers, Characters and Strings -- Valvano.files/Image18.gif"
width=251> </P>
<P> </P></DIR></DIR>
<P>where each bit b7,...,b0 is binary and has the value 1 or 0. We specify b7 as
the <I>most significant bit</I> or MSB, and b0 as the least significant bit or
LSB. If a byte is used to represent an unsigned number, then the value of the
number is</P>
<DIR>
<P>N = 128•b7 + 64•b6 + 32•b5 + 16•b4 + 8•b3 + 4•b2 + 2•b1 + b0</P></DIR>
<P>There are 256 different unsigned 8-bit numbers. The smallest unsigned 8-bit
number is 0 and the largest is 255. For example, 00001010<SUB>2</SUB> is 8+2 or
10. Other examples are shown in the following table.</P>
<P>
<TABLE cellSpacing=0 width=418 border=0>
<TBODY>
<TR>
<TD vAlign=top width="22%">binary</TD>
<TD vAlign=top width="20%">hex</TD>
<TD vAlign=top width="39%">Calculation</TD>
<TD vAlign=top width="19%">decimal</TD></TR>
<TR>
<TD vAlign=top width="22%">00000000</TD>
<TD vAlign=top width="20%">0x00</TD>
<TD vAlign=top width="39%"> </TD>
<TD vAlign=top width="19%">0</TD></TR>
<TR>
<TD vAlign=top width="22%">01000001</TD>
<TD vAlign=top width="20%">0x41</TD>
<TD vAlign=top width="39%">64+1</TD>
<TD vAlign=top width="19%">65</TD></TR>
<TR>
<TD vAlign=top width="22%">00010110</TD>
<TD vAlign=top width="20%">0x16</TD>
<TD vAlign=top width="39%">16+4+2</TD>
<TD vAlign=top width="19%">22</TD></TR>
<TR>
<TD vAlign=top width="22%">10000111</TD>
<TD vAlign=top width="20%">0x87</TD>
<TD vAlign=top width="39%">128+4+2+1</TD>
<TD vAlign=top width="19%">135</TD></TR>
<TR>
<TD vAlign=top width="22%">11111111</TD>
<TD vAlign=top width="20%">0xFF</TD>
<TD vAlign=top width="39%">128+64+32+16+8+4+2+1</TD>
<TD vAlign=top width="19%">255</TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 3-3. Example conversions from unsigned 8-bit binary to
hexadecimal and to decimal.</ADDRESS>
<P> </P>
<P>The <I>basis</I> of a number system is a subset from which linear
combinations of the basis elements can be used to construct the entire set. For
the unsigned 8-bit number system, the basis is</P>
<DIR>
<P>{ 1, 2, 4, 8, 16, 32, 64, 128}</P></DIR>
<P>One way for us to convert a decimal number into binary is to use the basis
elements. The overall approach is to start with the largest basis element and
work towards the smallest. One by one we ask ourselves whether or not we need
that basis element to create our number. If we do, then we set the corresponding
bit in our binary result and subtract the basis element from our number. If we
do not need it, then we clear the corresponding bit in our binary result. We
will work through the algorithm with the example of converting 100 to 8 bit
binary. We with the largest basis element (in this case 128) and ask whether or
not we need to include it to make 100. Since our number is less than 128, we do
not need it so bit 7 is zero. We go the next largest basis element, 64 and ask
do we need it. We do need 64 to generate our 100, so bit 6 is one and subtract
100 minus 64 to get 36. Next we go the next basis element, 32 and ask do we need
it. Again we do need 32 to generate our 36, so bit 5 is one and we subtract 36
minus 32 to get 4. Continuing along, we need basis element 4 but not 16 8 2 or
1, so bits 43210 are 00100 respectively. Putting it together we get 011001002
(which means 64+32+4).</P>
<P> </P>
<DIR>
<P>
<ADDRESS>Observation: If the least significant binary bit is zero, then the
number is even. <BR></ADDRESS>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -