I know what they do, I just don't understand when you'd get a use for them..
3 Answers
When you need to manipulate individual bits of a chunk of data (like a byte or an int). This happens frequently, for example, in algorithms dealing with:
- encryption
- compression
- audio / video processing
- networking (protocols)
- persistence (file formats)
- etc.

- 27,626
- 6
- 70
- 90
I've used them for bit masks before. Say you have an item that has a list of items that can have either a yes or no value (options on a car for instance). You can assign one integer column that will give a value for every option by assigning each option to a binary digit in the number.
Example: 5 = 101 in binary
that would mean:
option 1 - yes
option 2 - no
option 3 - yes
If you were to query on this you would use bitwise & or | operators to select the correct items.
Here is a good article that goes over it in more detail.

- 82,532
- 99
- 305
- 486
One example is if you have an (A)RGB color stored as a 32 bit integer and you want to extract the individual color components:
red = (rgb >> 16) & 0x000000ff;
green = (rgb >> 8) & 0x000000ff;
blue = rgb & 0x000000ff;
Of course as a high level programmer you would normally prefer to use a library function to do this rather than fiddling with bits yourself. But the library might be implemented using bitwise operations.

- 811,555
- 193
- 1,581
- 1,452
-
@DarkLightA: If you're asking why the operation is necassary, it's called masking - you only want to see the bits you are interested in - the rest are set to zero. http://en.wikipedia.org/wiki/Mask_%28computing%29#Masking_bits_to_0 If you are asking why the unnecessary zeros are there, it's just to be clear that we're dealing with 32-bit integers, but the leading zeros can be omitted if you wish. – Mark Byers Dec 26 '10 at 21:35