Lecture 23 Objectives
At the end of today's class you should
KNOW:
- the bitwise operators in C:
&, |, ^, <<, >>, ~
- that a mask is used in conjunction with the AND operator
to hide some bits while selecting others
- that bit operations are used to operate on bits of a data word for
applications such as device controllers
BE ABLE TO:
- manipulate int's using #defines and bitwise operators
- determine the value of a specific bit in a word
- set specific bits in a word without affecting the other bits in the word
- extract specific bits from a word
Sample Exam Question:
Write a function called Pack that satisfies the following pre- and post-conditions:
PRE: char1 and char2 are characters
POST: the function returns an unsigned short (i.e. 16-bit unsigned integer) with char1 packed into the
high-order 8 bits and char2 packed into the low-order 8 bits
You should use the shift and OR bit operations to accomplish the packing.