Posted 10 April 2012 - 02:15 AM
Masking, flags, whatever. There are a bunch of uses. Flags is the most straightforward and common. Say you have a bunch of boolean values to store. Keeping a variable for each is a pain. Instead, you can keep a single integer value, and have it hold 32 boolean values. How do you access individual bits for that? Bitwise operations. Say you declared it as "int flags;". If you want to read the flag 3rd bit, just use (flags & 0x4). Alternatively, if you need to compute the flag position, you can use shifts. (flags & (1<<3)) does the same thing. Setting a flag can be done with OR. flags|=0x04. Or you can specify a bunch of flags at once. flags=0x01|0x04|0x10. Typically, you'd have defines for each of these, so flags are named rather than numbered.
Another common thing is encoding/decoding compressed streams. Data in these usually has variable bit length, so a symbol may start in one byte and end in another. Then you need to pull specific bits to reconstruct the symbol. For specific examples, you can take a look at Deflate algorithm used in Zip, GZip, PNG, etc. Or for something a little more straight forward, GIF image encoding.