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

📄 chapter 3 numbers, characters and strings -- valvano.htm

📁 介绍了在嵌入式系统中如何用c来设计嵌入式软件
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<P>
<ADDRESS>&nbsp;</ADDRESS>
<P>
<ADDRESS>Observation: If the right most n bits (least significant) are zero, 
then the number is divisible by 2<SUP>n</SUP>. </ADDRESS>
<P>&nbsp;</P></DIR>
<P>
<TABLE cellSpacing=0 width=483 border=0>
  <TBODY>
  <TR>
    <TD vAlign=top width="14%">Number</TD>
    <TD vAlign=top width="13%">Basis</TD>
    <TD vAlign=top width="14%">Need it</TD>
    <TD vAlign=top width="16%">bit</TD>
    <TD vAlign=top width="43%">Operation</TD></TR>
  <TR>
    <TD vAlign=top width="14%">100</TD>
    <TD vAlign=top width="13%">128</TD>
    <TD vAlign=top width="14%">no</TD>
    <TD vAlign=top width="16%">bit7=0</TD>
    <TD vAlign=top width="43%">none</TD></TR>
  <TR>
    <TD vAlign=top width="14%">100</TD>
    <TD vAlign=top width="13%">64</TD>
    <TD vAlign=top width="14%">yes</TD>
    <TD vAlign=top width="16%">bit6=1</TD>
    <TD vAlign=top width="43%">subtract 100-64</TD></TR>
  <TR>
    <TD vAlign=top width="14%">36</TD>
    <TD vAlign=top width="13%">32</TD>
    <TD vAlign=top width="14%">yes</TD>
    <TD vAlign=top width="16%">bit5=1</TD>
    <TD vAlign=top width="43%">subtract 36-32</TD></TR>
  <TR>
    <TD vAlign=top width="14%">4</TD>
    <TD vAlign=top width="13%">16</TD>
    <TD vAlign=top width="14%">no</TD>
    <TD vAlign=top width="16%">bit4=0</TD>
    <TD vAlign=top width="43%">none</TD></TR>
  <TR>
    <TD vAlign=top width="14%">4</TD>
    <TD vAlign=top width="13%">8</TD>
    <TD vAlign=top width="14%">no</TD>
    <TD vAlign=top width="16%">bit3=0</TD>
    <TD vAlign=top width="43%">none</TD></TR>
  <TR>
    <TD vAlign=top width="14%">4</TD>
    <TD vAlign=top width="13%">4</TD>
    <TD vAlign=top width="14%">yes</TD>
    <TD vAlign=top width="16%">bit2=1</TD>
    <TD vAlign=top width="43%">subtract 4-4</TD></TR>
  <TR>
    <TD vAlign=top width="14%">0</TD>
    <TD vAlign=top width="13%">2</TD>
    <TD vAlign=top width="14%">no</TD>
    <TD vAlign=top width="16%">bit1=0</TD>
    <TD vAlign=top width="43%">none</TD></TR>
  <TR>
    <TD vAlign=top width="14%">0</TD>
    <TD vAlign=top width="13%">1</TD>
    <TD vAlign=top width="14%">no</TD>
    <TD vAlign=top width="16%">bit0=0</TD>
    <TD vAlign=top width="43%">none</TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 3-4. Example conversion from decimal to unsigned 8-bit binary to 
hexadecimal.</ADDRESS>
<P>We define an unsigned 8-bit number using the <CODE>unsigned char</CODE> 
format. When a number is stored into an <CODE>unsigned char</CODE> it is 
converted to 8-bit unsigned value. For example</P>
<DIR>
<P><CODE>unsigned char data; // 0 to 255<BR>unsigned char function(unsigned char 
input){<BR>&nbsp;&nbsp;&nbsp;&nbsp;data=input+1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;return 
data;}</CODE></P></DIR>
<P><B><I><FONT face=Helvetica,Arial><A name=BM8BITSIGNED></A>8-bit signed 
numbers</FONT></I></B></P>
<P>If a byte is used to represent a <I>signed 2&#8217;s complement</I> number, then 
the value of the number is</P>
<DIR>
<P>N = -128&#8226;b7 + 64&#8226;b6 + 32&#8226;b5 + 16&#8226;b4 + 8&#8226;b3 + 4&#8226;b2 + 2&#8226;b1 + b0</P></DIR>
<P>There are also 256 different signed 8 bit numbers. The smallest signed 8-bit 
number is -128 and the largest is 127. For example, 10000010<SUB>2</SUB> is 
-128+2 or -126. Other examples are shown in the following table.</P>
<P>
<TABLE cellSpacing=0 width=438 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%">&nbsp;</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%">-121</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%">-1</TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 3-5. Example conversions from signed 8-bit binary to hexadecimal 
and to decimal.</ADDRESS>
<P>For the signed 8-bit number system the basis is</P>
<DIR>
<P>{ 1, 2, 4, 8, 16, 32, 64, -128}</P>
<P>&nbsp;</P>
<P>
<ADDRESS>Observation: The most significant bit in a 2&#8217;s complement signed number 
will specify the sign. </ADDRESS>
<P>&nbsp;</P></DIR>
<P>Notice that the same binary pattern of 11111111<SUB>2</SUB> could represent 
either 255 or -1. It is very important for the software developer to keep track 
of the number format. The computer can not determine whether the 8-bit number is 
signed or unsigned. You, as the programmer, will determine whether the number is 
signed or unsigned by the specific assembly instructions you select to operate 
on the number. Some operations like addition, subtraction, and shift left 
(multiply by 2) use the same hardware (instructions) for both unsigned and 
signed operations. On the other hand, multiply, divide, and shift right (divide 
by 2) require separate hardware (instruction) for unsigned and signed 
operations. For example, the 6805/6808/6811 multiply instruction, <B>mul</B>, 
operates only on unsigned values. So if you use the <B>mul</B> instruction, you 
are implementing unsigned arithmetic. The Motorola 6812 has both unsigned, 
<B>mul</B>, and signed, <B>smul</B>, multiply instructions. So if you use the 
<B>smul</B> instruction, you are implementing signed arithmetic. The compiler 
will automatically choose the proper implementation.</P>
<P>It is always good programming practice to have clear understanding of the 
data type for each number, variable, parameter, etc. For some operations there 
is a difference between the signed and unsigned numbers while for others it does 
not matter. </P>
<P>
<TABLE cellSpacing=0 width=576 border=0>
  <TBODY>
  <TR>
    <TD vAlign=center></TD>
    <TD vAlign=center><U>signed different from unsigned</U></TD>
    <TD vAlign=center>&nbsp;</TD>
    <TD vAlign=center><U>signed same as unsigned</U></TD></TR>
  <TR>
    <TD vAlign=center>/ % </TD>
    <TD vAlign=center>division</TD>
    <TD vAlign=center>+</TD>
    <TD vAlign=center>addition</TD></TR>
  <TR>
    <TD vAlign=center>* </TD>
    <TD vAlign=center>multiplication</TD>
    <TD vAlign=center>- </TD>
    <TD vAlign=center>subtraction</TD></TR>
  <TR>
    <TD vAlign=center>&gt;</TD>
    <TD vAlign=center>greater than</TD>
    <TD vAlign=center>==</TD>
    <TD vAlign=center>is equal to</TD></TR>
  <TR>
    <TD vAlign=center>&lt;</TD>
    <TD vAlign=center>less than</TD>
    <TD vAlign=center>|</TD>
    <TD vAlign=center>logical or</TD></TR>
  <TR>
    <TD vAlign=center>&gt;=</TD>
    <TD vAlign=center>greater than or equal to</TD>
    <TD vAlign=center>&amp;</TD>
    <TD vAlign=center>logical and</TD></TR>
  <TR>
    <TD vAlign=center>&lt;=</TD>
    <TD vAlign=center>less than or equal to</TD>
    <TD vAlign=center>^</TD>
    <TD vAlign=center>logical exclusive or</TD></TR>
  <TR>
    <TD vAlign=center>&gt;&gt;</TD>
    <TD vAlign=center>right shift</TD>
    <TD vAlign=center>&lt;&lt;</TD>
    <TD vAlign=center>left shift</TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 3-6. Operations either depend or don't depend on whether the 
number is signed/unsigned.</ADDRESS>
<P>&nbsp;</P>
<P>The point is that care must be taken when dealing with a mixture of numbers 
of different sizes and types.</P>
<P>Similar to the unsigned algorithm, we can use the basis to convert a decimal 
number into signed binary. 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 decide do we need to include it to make -100. Yes (without -128, 
we would be unable to add the other basis elements together to get any negative 
result), so we set bit 7 and subtract the basis element from our value. Our new 
value is -100 minus -128, which is 28. We go the next largest basis element, 64 
and ask do we need it. We do not need 64 to generate our 28, so bit6 is zero. 
Next we go the next basis element, 32 and ask do we need it. We do not need 32 
to generate our 28, so bit5 is zero. Now we need the basis element 16, so we set 
bit4, and subtract 16 from our number 28 (28-16=12). Continuing along, we need 
basis elements 8 and 4 but not 2 1, so bits 3210 are 1100. Putting it together 
we get 100111002 (which means -128+16+8+4). </P>
<P>&nbsp;</P>
<P>
<TABLE cellSpacing=0 width=384 border=0>
  <TBODY>
  <TR>
    <TD vAlign=top width="17%">Number</TD>
    <TD vAlign=top width="15%">Basis</TD>
    <TD vAlign=top width="17%">Need it</TD>
    <TD vAlign=top width="19%">bit</TD>
    <TD vAlign=top width="32%">Operation</TD></TR>
  <TR>
    <TD vAlign=top width="17%">-100</TD>
    <TD vAlign=top width="15%">-128</TD>
    <TD vAlign=top width="17%">yes</TD>
    <TD vAlign=top width="19%">bit7=1</TD>
    <TD vAlign=top width="32%">subtract -100 - -128</TD></TR>
  <TR>
    <TD vAlign=top width="17%">28</TD>
    <TD vAlign=top width="15%">64</TD>
    <TD vAlign=top width="17%">no</TD>
    <TD vAlign=top width="19%">bit6=0</TD>
    <TD vAlign=top width="32%">none</TD></TR>
  <TR>
    <TD vAlign=top width="17%">28</TD>
    <TD vAlign=top width="15%">32</TD>
    <TD vAlign=top width="17%">no</TD>
    <TD vAlign=top width="19%">bit5=0</TD>
    <TD vAlign=top width="32%">none</TD></TR>
  <TR>
    <TD vAlign=top width="17%">28</TD>
    <TD vAlign=top width="15%">16</TD>
    <TD vAlign=top width="17%">yes</TD>
    <TD vAlign=top width="19%">bit4=1</TD>
    <TD vAlign=top width="32%">subtract 28-16</TD></TR>
  <TR>
    <TD vAlign=top width="17%">12</TD>
    <TD vAlign=top width="15%">8</TD>
    <TD vAlign=top width="17%">yes</TD>
    <TD vAlign=top width="19%">bit3=1</TD>
    <TD vAlign=top width="32%">subtract 12-8</TD></TR>
  <TR>
    <TD vAlign=top width="17%">4</TD>
    <TD vAlign=top width="15%">4</TD>
    <TD vAlign=top width="17%">yes</TD>
    <TD vAlign=top width="19%">bit2=1</TD>
    <TD vAlign=top width="32%">subtract 4-4</TD></TR>
  <TR>
    <TD vAlign=top width="17%">0</TD>
    <TD vAlign=top width="15%">2</TD>
    <TD vAlign=top width="17%">no</TD>
    <TD vAlign=top width="19%">bit1=0</TD>
    <TD vAlign=top width="32%">none</TD></TR>
  <TR>

⌨️ 快捷键说明

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