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

📄 chapter 5 expressions -- valvano.htm

📁 介绍了在嵌入式系统中如何用c来设计嵌入式软件
💻 HTM
📖 第 1 页 / 共 4 页
字号:
  <TR>
    <TD vAlign=top width="5%"><CODE>*</CODE></TD>
    <TD vAlign=top>reference</TD>
    <TD vAlign=center width="32%"><FONT face=Monaco>*pt</FONT></TD>
    <TD vAlign=center width="32%">16 bit information pointed to by 
  pt</TD></TR></TBODY></TABLE></P>
<ADDRESS>&nbsp;Table 5.1: Unary prefix operators.<FONT 
face="Times New Roman,Times"> </FONT></ADDRESS>
<P>
<TABLE cellSpacing=0 border=0>
  <TBODY>
  <TR>
    <TD vAlign=top width="5%">
      <ADDRESS><CODE>operator</CODE></ADDRESS></TD>
    <TD vAlign=top width="32%">
      <ADDRESS><CODE>meaning</CODE></ADDRESS></TD>
    <TD vAlign=center width="32%"><I>example</I></TD>
    <TD vAlign=center width="32%"><I>result</I></TD></TR>
  <TR>
    <TD vAlign=top width="5%"><CODE>++</CODE></TD>
    <TD vAlign=top width="32%">postincrement </TD>
    <TD vAlign=center width="32%"><FONT face=Monaco>data++</FONT> </TD>
    <TD vAlign=center width="32%">result is data, then data=data+1</TD></TR>
  <TR>
    <TD vAlign=top width="5%"><CODE>--</CODE></TD>
    <TD vAlign=top width="32%">postdecrement</TD>
    <TD vAlign=center width="32%"><FONT face=Monaco>data--</FONT></TD>
    <TD vAlign=center width="32%">result is data, then 
  data=data+1</TD></TR></TBODY></TABLE></P>
<ADDRESS>&nbsp;Table 5.2: Unary postfix operators.<FONT 
face="Times New Roman,Times"> </FONT></ADDRESS>
<ADDRESS>&nbsp;</ADDRESS>
<P><B><I><FONT face=Helvetica,Arial><A name=BINARY></A>Binary 
operators</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">Next we list the binary arithmetic 
operators, which operate on two number inputs giving a single number result. The 
operations of addition, subtraction and shift left are the same independent of 
whether the numbers are signed or unsigned. As we will see later, </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#OVERFLOW">overflow 
and underflow</A><FONT face="Times New Roman,Times"> after an addition, 
subtraction and shift left are different for signed and unsigned numbers, but 
the operation itself is the same. On the other hand multiplication, division, 
and shift right have different functions depending on whether the numbers are 
signed or unsigned. It will be important, therefore, to avoid multiplying or 
dividing an unsigned number with a signed number. </FONT></P>
<P>
<TABLE cellSpacing=0 border=0>
  <TBODY>
  <TR>
    <TD vAlign=top width="13%">
      <ADDRESS><CODE>operator</CODE> </ADDRESS></TD>
    <TD vAlign=top width="87%">
      <ADDRESS><CODE>meaning</CODE></ADDRESS></TD>
    <TD vAlign=center width="87%"><I>example</I></TD>
    <TD vAlign=center width="87%"><I>result</I></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>+</CODE></TD>
    <TD vAlign=top width="87%">addition</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>100+300</FONT></TD>
    <TD vAlign=center width="87%">400</TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>-</CODE></TD>
    <TD vAlign=top width="87%">subtraction</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>100-300</FONT></TD>
    <TD vAlign=center width="87%">-200</TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>*</CODE></TD>
    <TD vAlign=top width="87%">multiplication</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>10*300</FONT></TD>
    <TD vAlign=center width="87%">3000</TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>/</CODE></TD>
    <TD vAlign=top width="87%">division</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>123/10</FONT></TD>
    <TD vAlign=center width="87%">12</TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>%</CODE></TD>
    <TD vAlign=top width="87%">remainder</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>123%10</FONT></TD>
    <TD vAlign=center width="87%">3</TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>&lt;&lt;</CODE></TD>
    <TD vAlign=top width="87%">shift left</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>102&lt;&lt;2</FONT></TD>
    <TD vAlign=center width="87%">408</TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>&gt;&gt;</CODE></TD>
    <TD vAlign=top width="87%">shift right</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>102&gt;&gt;2</FONT></TD>
    <TD vAlign=center width="87%">25</TD></TR></TBODY></TABLE></P>
<ADDRESS>&nbsp;Table 5.3: Binary arithmetic operators.<FONT 
face="Times New Roman,Times"> </FONT></ADDRESS>
<P>&nbsp;</P>
<P><FONT face="Times New Roman,Times">The binary bitwise logical operators take 
two inputs and give a single result.</FONT></P>
<P>
<TABLE cellSpacing=0 border=0>
  <TBODY>
  <TR>
    <TD vAlign=top width="13%">
      <ADDRESS><CODE>operator</CODE></ADDRESS></TD>
    <TD vAlign=top width="87%">
      <ADDRESS><CODE>meaning</CODE></ADDRESS></TD>
    <TD vAlign=center width="87%"><I>example</I></TD>
    <TD vAlign=center width="87%"><I>result</I></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>&amp;</CODE></TD>
    <TD vAlign=top width="87%">bitwise and</TD>
    <TD vAlign=center width="87%"><FONT 
face=Monaco>0x1234&amp;0x00FF</FONT></TD>
    <TD vAlign=center width="87%">0x0034</TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>|</CODE></TD>
    <TD vAlign=top width="87%">bitwise or</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>0x1234|0x00FF</FONT></TD>
    <TD vAlign=center width="87%">0x12FF</TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>^</CODE></TD>
    <TD vAlign=top width="87%">bitwise exclusive or</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>0x1234^0x00FF</FONT></TD>
    <TD vAlign=center width="87%">0x12CB</TD></TR></TBODY></TABLE></P>
<ADDRESS>&nbsp;Table 5.4: Binary bitwise logical operators.<FONT 
face="Times New Roman,Times"> </FONT></ADDRESS>
<ADDRESS>&nbsp;</ADDRESS>
<P><FONT face="Times New Roman,Times"><A name=BOOLEAN></A>The binary boolean 
operators take two boolean inputs and give a single boolean result.</FONT></P>
<P>
<TABLE cellSpacing=0 border=0>
  <TBODY>
  <TR>
    <TD vAlign=top width="13%">
      <ADDRESS><CODE>operator</CODE></ADDRESS></TD>
    <TD vAlign=top width="87%">
      <ADDRESS><CODE>meaning</CODE></ADDRESS></TD>
    <TD vAlign=center width="87%"><I>example</I></TD>
    <TD vAlign=center width="87%"><I>result</I></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>&amp;&amp;</CODE></TD>
    <TD vAlign=top width="87%">and</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>0 &amp;&amp; 1</FONT></TD>
    <TD vAlign=center width="87%">0 (false)</TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>||</CODE></TD>
    <TD vAlign=top width="87%">or</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>0 || 1</FONT></TD>
    <TD vAlign=center width="87%">1 (true)</TD></TR></TBODY></TABLE></P>
<ADDRESS>&nbsp;Table 5.5: Binary boolean operators.<FONT 
face="Times New Roman,Times"> </FONT></ADDRESS>
<ADDRESS>&nbsp;</ADDRESS>
<P><FONT face="Times New Roman,Times">Many programmers confuse the logical 
operators with the boolean operators. Logical operators take two numbers and 
perform a bitwise logical operation. Boolean operators take two boolean inputs 
(0 and notzero) and return a boolean (0 or 1)</FONT>. In the program below, the 
operation <CODE>c=a&amp;b;</CODE> will perform a <I>bitwise logical and</I> of 
0x0F0F <B>and</B> 0xF0F0 resulting in 0x0000. In the 
<CODE>d=a&amp;&amp;b;</CODE> expression, the value a is considered as a true 
(because it is not zero) and the value b also is considered a true (not zero). 
The boolean operation of true <B>and</B> true gives a true result 
(1).<BR><CODE>int a,b,c,d;</CODE><FONT face="Courier,Courier New" color=#008000 
size=2><BR></FONT><CODE>void main(void){ a=0x0F0F; b=F0F0; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;c=a&amp;b;&nbsp;&nbsp;/* logical result c will be 
0x0000 */ <BR>&nbsp;&nbsp;&nbsp;&nbsp;d=a&amp;&amp;b; /* boolean result d will 
be 1 (true) */<BR>}</CODE></P>
<P><I>Listing 5-1: Illustration of the difference between logical and boolean 
operators</I></P>
<P><FONT face="Times New Roman,Times"><A name=RELATIONAL></A>The binary 
relational operators take two number inputs and give a single boolean 
result.</FONT></P>
<P>
<TABLE cellSpacing=0 border=0>
  <TBODY>
  <TR>
    <TD vAlign=top width="13%">
      <ADDRESS><CODE>operator</CODE></ADDRESS></TD>
    <TD vAlign=top width="87%">
      <ADDRESS><CODE>meaning</CODE></ADDRESS></TD>
    <TD vAlign=center width="87%"><I>example</I></TD>
    <TD vAlign=center width="87%"><I>result</I></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>==</CODE></TD>
    <TD vAlign=top width="87%">equal</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>100 == 200</FONT></TD>
    <TD vAlign=center width="87%">0 (false)</TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>!=</CODE></TD>
    <TD vAlign=top width="87%">not equal</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>100 != 200</FONT></TD>
    <TD vAlign=center width="87%">1 (true)</TD></TR>
  <TR>
    <TD vAlign=top width="13%">&lt;</TD>
    <TD vAlign=top width="87%">less than</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>100 &lt; 200</FONT></TD>
    <TD vAlign=center width="87%">1 (true)</TD></TR>
  <TR>
    <TD vAlign=top width="13%">&lt;=</TD>
    <TD vAlign=top width="87%">less than or equal</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>100 &lt;= 200</FONT></TD>
    <TD vAlign=center width="87%">1 (true)</TD></TR>
  <TR>
    <TD vAlign=top width="13%">&gt;</TD>
    <TD vAlign=top width="87%">greater than</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>100 &gt; 200</FONT></TD>
    <TD vAlign=center width="87%">0 (false)</TD></TR>
  <TR>
    <TD vAlign=top width="13%">&gt;=</TD>
    <TD vAlign=top width="87%">greater than or equal</TD>
    <TD vAlign=center width="87%"><FONT face=Monaco>100 &gt;= 200</FONT></TD>
    <TD vAlign=center width="87%">0 (false)</TD></TR></TBODY></TABLE></P>
<ADDRESS>&nbsp;Table 5.6: Binary relational operators.<FONT 
face="Times New Roman,Times"> </FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">Some programmers confuse <I>assignment 
equals</I> with the <I>relational equals</I>. In the following example, the 
first <B>if</B> will execute the </FONT><CODE>subfunction()</CODE><FONT 
face="Times New Roman,Times"> if a is equal to zero (a is not modified). In the 
second case, the variable b is set to zero, and the 
</FONT><CODE>subfunction()</CODE><FONT face="Times New Roman,Times"> will never 
be executed because the result of the equals assignment is the value (in this 
case the 0 means false)</FONT>.<BR><CODE>int a,b;</CODE><FONT 
face="Courier,Courier New" color=#008000 size=2><BR></FONT><CODE>void 
program(void){ <BR>&nbsp;&nbsp;&nbsp;&nbsp;if(a==0) subfunction();&nbsp;/* 
execute subfunction if a is zero */ <BR>&nbsp;&nbsp;&nbsp;&nbsp;if(b=0) 
subfunction();&nbsp;&nbsp;/* set b to zero, never execute subfunction 
*/<BR>}</CODE></P>
<P><I>Listing 5-2: Illustration of the difference between relational and 
assignment equals</I></P>
<P><FONT face="Times New Roman,Times">Before looking at the kinds of expressions 
we can write in C, we will first consider the process of evaluating expressions 
and some general properties of operators. </FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A name=ASSIGNMENT></A>Assignment 

⌨️ 快捷键说明

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