Page 43 - Programming Microcontrollers in C
P. 43
28 Chapter 1 Introduction to C
greater width operand prior to execution of the operation. Thus, if
the program called for d = a * b, where d is of type long, a is
type int, and b is type long, a will be converted to the type long
prior to the multiplication.
This logic carries over to mixing of float and double types
as well. If for example a program called for the division a/b where
a is of the type int and b is of the type double, the program would
convert a to the type double before execution of the divide.
There might be times when the programmer will want to change
the type of a variable. C provides a cast operator which forces the
program to convert the type of a variable to a different type. This
unary operator has the form.
(type name) expression
where the results of the evaluation of the expression will be con
verted to the named type contained within the parentheses preceding
the expression.
Bitwise Operators
Operators that work on the individual bits within a variable are
called bitwise operators. Following is a table of all of these opera
tors:
& bitwise AND >> right shift
| bitwise Inclusive OR << left shift
^ bitwise Exclusive OR ~ one’s complement
The first three bitwise operators are traditional binary operators.
These binary operators operate in integer type (char, int, long,
etc.) operands, and the two operands must be of the same type.
If a bitwise AND is executed, those locations in the result where
both operands have bit values of 1 will have a value of 1. All other
locations will be 0. For a bitwise inclusive OR, each bit in the
result will be 1 when either or both operand bits are 1. All locations
where both operand bits are 0 will be 0. The exclusive OR is
similar to an addition with no carry. Whenever the bits in the oper
ands are different, the result bit will be 1. If both operand bits are the
same, either both bits 1 or both bits 0, the result will be 0.