📄 chapter 3 numbers, characters and strings -- valvano.htm
字号:
<TD vAlign=top width="17%">0</TD>
<TD vAlign=top width="15%">1</TD>
<TD vAlign=top width="17%">no</TD>
<TD vAlign=top width="19%">bit0=0</TD>
<TD vAlign=top width="32%">none</TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 3-7. Example conversion from decimal to signed 8-bit binary to
hexadecimal.</ADDRESS>
<P> </P>
<DIR>
<P>
<ADDRESS>Observation: To take the negative of a 2’s complement signed number we
first complement (flip) all the bits, then add 1. </ADDRESS>
<DIR>
<P> </P></DIR></DIR>
<P>A second way to convert negative numbers into binary is to first convert them
into unsigned binary, then do a 2’s complement negate. For example, we earlier
found that +100 is 011001002. The 2’s complement negate is a two step process.
First we do a logic complement (flip all bits) to get 100110112. Then add one to
the result to get 100111002. </P>
<P>A third way to convert negative numbers into binary is to first subtract the
number from 256, then convert the unsigned result to binary using the unsigned
method. For example, to find -100, we subtract 256 minus 100 to get 156. Then we
convert 156 to binary resulting in 100111002. This method works because in 8 bit
binary math adding 256 to number does not change the value. E.g., 256-100 is the
same value as -100. </P>
<DIR>
<P>
<ADDRESS>Common Error: An error will occur if you use signed operations on
unsigned numbers, or use unsigned operations on signed numbers. </ADDRESS>
<P>
<ADDRESS> </ADDRESS>
<P>
<ADDRESS>Maintenance Tip: To improve the clarity of our software, always specify
the format of your data (signed versus unsigned) when defining or accessing the
data. </ADDRESS></DIR>
<P>We define a signed 8-bit number using the <CODE>char</CODE> format. When a
number is stored into a <CODE>char</CODE> it is converted to 8-bit signed value.
For example</P>
<DIR>
<P><CODE>char data; // -128 to 127<BR>char function(char
input){<BR> data=input+1;<BR> return
data;}</CODE></P>
<P> </P></DIR>
<P><B><I><FONT face=Helvetica,Arial><A name=BM16BITUNSIGNED></A>16 bit unsigned
numbers</FONT></I></B></P>
<P>A <I>word</I> or <I>double byte</I> contains 16 bits</P>
<DIR>
<DIR>
<P><IMG height=32
src="Chapter 3 Numbers, Characters and Strings -- Valvano.files/Image19.gif"
width=335></P>
<P> </P></DIR></DIR>
<P>where each bit b15,...,b0 is binary and has the value 1 or 0. If a word is
used to represent an unsigned number, then the value of the number is</P>
<DIR>
<P>N = 32768•b15 + 16384•b14 + 8192•b13 + 4096•b12 </P>
<P>+ 2048•b11 + 1024•b10 + 512•b9 + 256•b8</P>
<P>+ 128•b7 + 64•b6 + 32•b5 + 16•b4 + 8•b3 + 4•b2 + 2•b1 + b0</P></DIR>
<P>There are 65,536 different unsigned 16-bit numbers. The smallest unsigned
16-bit number is 0 and the largest is 65535. For example,
0010,0001,1000,0100<SUB>2</SUB> or 0x2184 is 8192+256+128+4 or 8580. Other
examples are shown in the following table.</P>
<P> </P>
<P>
<TABLE cellSpacing=0 width=486 border=0>
<TBODY>
<TR>
<TD vAlign=top width="27%">binary</TD>
<TD vAlign=top width="14%">hex</TD>
<TD vAlign=top width="43%">Calculation</TD>
<TD vAlign=top width="15%">decimal</TD></TR>
<TR>
<TD vAlign=top width="27%"><FONT size=2>0000,0000,0000,0000</FONT></TD>
<TD vAlign=top width="14%"><FONT size=2>0x0000</FONT></TD>
<TD vAlign=top width="43%"> </TD>
<TD vAlign=top width="15%"><FONT size=2>0</FONT></TD></TR>
<TR>
<TD vAlign=top width="27%"><FONT size=2>0000,0100,0000,0001</FONT></TD>
<TD vAlign=top width="14%"><FONT size=2>0x0401</FONT></TD>
<TD vAlign=top width="43%"><FONT size=2>1024+1</FONT></TD>
<TD vAlign=top width="15%"><FONT size=2>1025</FONT></TD></TR>
<TR>
<TD vAlign=top width="27%"><FONT size=2>0000,1100,1010,0000</FONT></TD>
<TD vAlign=top width="14%"><FONT size=2>0x0CA0</FONT></TD>
<TD vAlign=top width="43%"><FONT size=2>2048+1024+128+32</FONT></TD>
<TD vAlign=top width="15%"><FONT size=2>3232</FONT></TD></TR>
<TR>
<TD vAlign=top width="27%"><FONT size=2>1000,1110,0000,0010</FONT></TD>
<TD vAlign=top width="14%"><FONT size=2>0x8E02</FONT></TD>
<TD vAlign=top width="43%"><FONT size=2>32768+2048+1024+512+2</FONT></TD>
<TD vAlign=top width="15%"><FONT size=2>36354</FONT></TD></TR>
<TR>
<TD vAlign=top width="27%"><FONT size=2>1111,1111,1111,1111</FONT></TD>
<TD vAlign=top width="14%"><FONT size=2>0xFFFF</FONT></TD>
<TD vAlign=top width="43%"><FONT size=2>32768+16384+8192+4096+2048+1024
+512+256+128+64+32+16+8+4+2+1</FONT></TD>
<TD vAlign=top width="15%"><FONT
size=2>65535</FONT></TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 3-8. Example conversions from unsigned 16-bit binary to
hexadecimal and to decimal.</ADDRESS>
<P> </P>
<P>For the unsigned 16-bit number system the basis is</P>
<DIR>
<P>{ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384,
32768}</P></DIR>
<P>If a word is used to represent a signed 2’s complement number, then the value
of the number is</P>
<DIR>
<P>N = -32768•b15 + 16384•b14 + 8192•b13 + 4096•b12 </P>
<P>+ 2048•b11 + 1024•b10 + 512•b9 + 256•b8</P>
<P>+ 128•b7 + 64•b6 + 32•b5 + 16•b4 + 8•b3 + 4•b2 + 2•b1 + b0</P></DIR>
<P>We define an unsigned 16-bit number using the <CODE>unsigned short</CODE>
format. When a number is stored into an <CODE>unsigned short</CODE> it is
converted to 16-bit unsigned value. For example</P>
<DIR>
<P><CODE>unsigned short data; // 0 to 65535<BR>unsigned short function(unsigned
short
input){<BR> data=input+1;<BR> return
data;}</CODE></P></DIR>
<P><B><I><FONT face=Helvetica,Arial><A name=BM16BITSIGNED></A>16-bit signed
numbers</FONT></I></B></P>
<P>There are also 65,536 different signed 16-bit numbers. The smallest signed
16-bit number is -32768 and the largest is 32767. For example,
1101,0000,0000,0100<SUB>2</SUB> or 0xD004 is -32768+16384+4096+4 or -12284.
Other examples are shown in the following table.</P>
<P>
<TABLE cellSpacing=0 width=526 border=0>
<TBODY>
<TR>
<TD vAlign=top width="25%">binary</TD>
<TD vAlign=top width="13%">hex</TD>
<TD vAlign=top width="40%">Calculation</TD>
<TD vAlign=top width="22%">decimal</TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>0000,0000,0000,0000</FONT></TD>
<TD vAlign=top width="13%"><FONT size=2>0x0000</FONT></TD>
<TD vAlign=top width="40%"><FONT size=2> </FONT></TD>
<TD vAlign=top width="22%"><FONT size=2>0</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>0000,0100,0000,0001</FONT></TD>
<TD vAlign=top width="13%"><FONT size=2>0x0401</FONT></TD>
<TD vAlign=top width="40%"><FONT size=2>1024+1</FONT></TD>
<TD vAlign=top width="22%"><FONT size=2>1025</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>0000,1100,1010,0000</FONT></TD>
<TD vAlign=top width="13%"><FONT size=2>0x0CA0</FONT></TD>
<TD vAlign=top width="40%"><FONT size=2>2048+1024+128+32</FONT></TD>
<TD vAlign=top width="22%"><FONT size=2>3232</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>1000,0100,0000,0010</FONT></TD>
<TD vAlign=top width="13%"><FONT size=2>0x8402</FONT></TD>
<TD vAlign=top width="40%"><FONT size=2>-32768+1024+2</FONT></TD>
<TD vAlign=top width="22%"><FONT size=2>-31742</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>1111,1111,1111,1111</FONT></TD>
<TD vAlign=top width="13%"><FONT size=2>0xFFFF</FONT></TD>
<TD vAlign=top width="40%"><FONT size=2>-32768+16384+8192+4096+2048+1024
+512+256+128+64+32+16+8+4+2+1</FONT></TD>
<TD vAlign=top width="22%"><FONT size=2>-1</FONT></TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 3-9. Example conversions from signed 16-bit binary to hexadecimal
and to decimal.</ADDRESS>
<P> </P>
<P>For the signed 16-bit number system the basis is</P>
<DIR>
<P>{ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384,
-32768}</P>
<P><I>Maintenance Tip: To improve the quality of our software, we should always
specify the precision of our data when defining or accessing the data.</I>
</P></DIR>
<P>We define a signed 16-bit number using the<CODE> short</CODE> format. When a
number is stored into a <CODE>short</CODE> it is converted to 16-bit signed
value. For example</P>
<DIR>
<P><CODE>short data; // -23768 to 32767<BR>short function(short
input){<BR> data=input+1;<BR> return
data;}</CODE></P></DIR>
<P><B><I><FONT face=Helvetica,Arial><A name=ENDIAN></A>Big and Little
Endian</FONT></I></B></P>
<P>When we store 16-bit data into memory it requires two bytes. Since the memory
systems on most computers are byte addressable (a unique address for each byte),
there are two possible ways to store in memory the two bytes that constitute the
16-bit data. Motorola microcomputers implement the <I>big endian</I> approach
that stores the most significant part first. Intel microcomputers implement the
<I>little endian</I> approach that stores the least significant part first. The
PowerPC is <I>biendian</I>, because it can be configured to efficiently handle
both big and little endian. For example, assume we wish to store the 16 bit
number 1000 (0x03E8) at locations 0x50,0x51, then </P>
<P> </P>
<CENTER>
<P><IMG height=82
src="Chapter 3 Numbers, Characters and Strings -- Valvano.files/Image20.gif"
width=268></P>
<P> </P></CENTER>
<P>We also can use either the big or little endian approach when storing 32-bit
numbers into memory that is byte (8-bit) addressable. If we wish to store the
32-bit number 0x12345678 at locations 0x50-0x53 then </P>
<CENTER>
<P> </P>
<P><IMG height=85
src="Chapter 3 Numbers, Characters and Strings -- Valvano.files/Image21.gif"
width=216></P>
<P> </P></CENTER>
<P>In the above two examples we normally would not pick out individual bytes
(e.g., the 0x12), but rather capture the entire multiple byte data as one
nondivisable piece of information. On the other hand, if each byte in a multiple
byte data structure is individually addressable, then both the big and little
endian schemes store the data in first to last sequence. For example, if we wish
to store the 4 ASCII characters ‘6811’ which is 0x36383131 at locations
0x50-0x53, then the ASCII ‘6’=0x36 comes first in both big and little endian
schemes. </P>
<CENTER>
<P> </P>
<P><IMG height=82
src="Chapter 3 Numbers, Characters and Strings -- Valvano.files/Image22.gif"
width=171></P>
<P> </P></CENTER>
<P>The term "Big Endian" comes from Jonathan Swift’s satire <U>Gulliver’s
Travels</U>. In Swift’s book, a Big Endian refers to a person who cracks their
egg on the big end. The Lilliputians considered the big endians as inferiors.
The big endians fought a long and senseless war with the Lilliputians who
insisted it was only proper to break an egg on the little end. </P>
<P> </P>
<DIR>
<P>
<ADDRESS>Common Error: An error will occur when data is stored in Big Endian by
one computer and read in Little Endian format on another. </ADDRESS>
<P> </P></DIR>
<P><B><I><FONT face=Helvetica,Arial><A name=BOOLEAN></A>Boolean
information</FONT></I></B></P>
<P>A boolean number is has two states. The two values could represent the
logical true or false. The positive logic representation defines true as a 1 or
high, and false as a 0 or low. If you were controlling a motor, light, heater or
air conditioner the boolean could mean on or off. In communication systems, we
represent the information as a sequence of booleans: mark or space. For black or
white graphic displays we use booleans to specify the state of each pixel. The
most efficient storage of booleans on a computer is to map each boolean into one
memory bit. In this way, we could pack 8 booleans into each byte. If we have
just one boolean to store in memory, out of convenience we allocate an entire
byte or word for it. Most C compilers including ICC11/ICC12/Hiware define: </P>
<DIR>
<P>False be all zeros, and<BR>True be any nonzero value.</P></DIR>
<P>Many programmers add the following macros</P>
<DIR>
<P><CODE>#define TRUE 1<BR>#define FALSE 0</CODE></P>
<P> </P></DIR>
<P><B><I><FONT face=Helvetica,Arial><A name=DECIMAL></A>Decimal
Numbers</FONT></I></B></P>
<P>Decimal numbers are written as a sequence of decimal digits (0 through 9).
The number may be preceded by a plus or minus sign or followed by a <B>L</B>or
<B>U</B>. Lower case <B>l </B>or <B>u </B>could also be used. The minus sign
gives the number a negative value, otherwise it is positive. The plus sign is
optional for positive values. Unsigned 16-bit numbers between 32768 and 65535
should be followed by <B>U</B>. You can place a <B>L</B>at the end of the number
to signify it to be a 32-bit signed number. The range of a decimal number
depends on the data type as shown in the following table.</P>
<P>
<TABLE cellSpacing=0 cellPadding=0 width=480 border=0>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -