📄 chapter 5 expressions -- valvano.htm
字号:
<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> 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> Table 5.2: Unary postfix operators.<FONT
face="Times New Roman,Times"> </FONT></ADDRESS>
<ADDRESS> </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><<</CODE></TD>
<TD vAlign=top width="87%">shift left</TD>
<TD vAlign=center width="87%"><FONT face=Monaco>102<<2</FONT></TD>
<TD vAlign=center width="87%">408</TD></TR>
<TR>
<TD vAlign=top width="13%"><CODE>>></CODE></TD>
<TD vAlign=top width="87%">shift right</TD>
<TD vAlign=center width="87%"><FONT face=Monaco>102>>2</FONT></TD>
<TD vAlign=center width="87%">25</TD></TR></TBODY></TABLE></P>
<ADDRESS> Table 5.3: Binary arithmetic operators.<FONT
face="Times New Roman,Times"> </FONT></ADDRESS>
<P> </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>&</CODE></TD>
<TD vAlign=top width="87%">bitwise and</TD>
<TD vAlign=center width="87%"><FONT
face=Monaco>0x1234&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> Table 5.4: Binary bitwise logical operators.<FONT
face="Times New Roman,Times"> </FONT></ADDRESS>
<ADDRESS> </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>&&</CODE></TD>
<TD vAlign=top width="87%">and</TD>
<TD vAlign=center width="87%"><FONT face=Monaco>0 && 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> Table 5.5: Binary boolean operators.<FONT
face="Times New Roman,Times"> </FONT></ADDRESS>
<ADDRESS> </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&b;</CODE> will perform a <I>bitwise logical and</I> of
0x0F0F <B>and</B> 0xF0F0 resulting in 0x0000. In the
<CODE>d=a&&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> c=a&b; /* logical result c will be
0x0000 */ <BR> d=a&&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%"><</TD>
<TD vAlign=top width="87%">less than</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%"><=</TD>
<TD vAlign=top width="87%">less than or 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%">></TD>
<TD vAlign=top width="87%">greater than</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%">>=</TD>
<TD vAlign=top width="87%">greater than or equal</TD>
<TD vAlign=center width="87%"><FONT face=Monaco>100 >= 200</FONT></TD>
<TD vAlign=center width="87%">0 (false)</TD></TR></TBODY></TABLE></P>
<ADDRESS> 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> if(a==0) subfunction(); /*
execute subfunction if a is zero */ <BR> if(b=0)
subfunction(); /* 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 + -