Sams Teach Yourself C in 21 Days

(singke) #1
If there’s no overlap,memmove()works just like memcpy(). With overlap, however,
memmove()copies the original source characters to the destination.

582 Day 20

DOusememmove()instead of memcpy()in
case you’re dealing with overlapping
memory regions.

DON’Ttry to use memset()to initialize
typeint,float, or doublearrays to any
value other than 0.

DO DON’T


Working with Bits ..............................................................................................


As you may know, the most basic unit of computer data storage is the bit. There are
times when being able to manipulate individual bits in your C program’s data is very
useful. C has several tools that let you do this.
The C bitwise operators let you manipulate the individual bits of integer variables.
Remember, a bitis the smallest possible unit of data storage, and it can have only one of
two values: 0 or 1. The bitwise operators can be used only with integer types:char,int,
andlong. Before continuing with this section, you should be familiar with binary nota-
tion—the way the computer internally stores integers. If you need to review binary nota-
tion, refer to Appendix C, “Working with Binary and Hexadecimal Numbers.”
The bitwise operators are most frequently used when your C program interacts directly
with your system’s hardware—a topic that is beyond the scope of this book. They do
have other uses, however, which we will introduce.

The Shift Operators ......................................................................................

Two shift operators shift the bits in an integer variable by a specified number of posi-
tions. The <<operator shifts bits to the left, and the >>operator shifts bits to the right.
The syntax for these binary operators is
x << n
and
x >> n
Each operator shifts the bits in xbynpositions in the specified direction. For a right
shift, zeros are placed in the nhigh-order bits of the variable; for a left shift, zeros are
placed in the nlow-order bits of the variable. Here are a few examples:

32 448201x-CH20 8/13/02 11:16 AM Page 582

Free download pdf