Page 49 - Programming Microcontrollers in C
P. 49
34 Chapter 1 Introduction to C
3. Write a program that will rotate the bits in the number 0x5aa5 to
the left by n bits. A rotate differs from a shift in that the most
significant bit will be shifted into the least significant bit during
the rotation. A shift merely shifts zeros into the least significant
bit.
4. An arithmetic right shift propagates the most significant bit to the
right when the number is shifted right. If zeros are shifted into the
most significant bit, the shift is called a logical right shift. Write a
program that determines whether your compiler implements a logi
cal or arithmetic right shift with the operator >> with both signed
and unsigned arithmetic.
5. Write a function upper(c) that returns the upper case letter if the
character c is a lower case letter. Otherwise it shall return the char
acter c.
6. If you used the if() else construct in problem 4, rewrite the func
tion to use the conditional expression.
Precedence and Associativity
Here is a summary of the rules of both precedence and associa
tion of all C operators. The higher an operator falls in the table, the
higher its precedence. Operators that fall on the same line are all of
the same precedence. All symbols used in C are operators. There
fore, the operator () refers to the parentheses enclosing the arguments
to a function call. The operator [] refers to the brackets enclosing the
argument of an array. The period operator . and the comma operator ,
will both be discussed when introduced. Likewise, the -> and the
sizeof operators will be introduced later.
Operator Associativity
() [] -> . left to right
! ~ ++ — + - * & (type) sizeof right to left
* / % left to right
+ - left to right
<< >> left to right