## What are bitwise operations?

Bitwise operations modify the pattern of bits in an object. To understand bitwise operations, it is necessary to have some background in binary numbers. A binary number consists of ones and zeros, which can, in the case of computers, represent two different states, such as on/off. For our purposes, though, it is best to think of it as merely a different way of writing a "normal" base ten number. In base ten, each of the digits in a number represents a power of ten. Let's use the number 432 as an example. The 2 represents the number of 10^0 (ones) there are in the number. Likewise, the 3 represents the number of 10^1 (tens) and the 4 represents the number of 10^2 (hundreds). Hence the number is actually 4*100 + 3*10 + 2*1.

Binary numbers, of course, work in exactly the same way. In the number 111, there is one 2^0 (one), one 2^1 (two), and one 2^2 (four). The number is a binary version of the base ten number 7 (4+2+1).

This brings us to the use of binary numbers in graphics programming. One of the most common uses of binary numbers is in defining colors in a colormaps. The color red, for example, could be defined in 24-bit color as 111111110000000000000000. This color has three areas or fields, which represent the red, green, and blue values for the color. The first eight bits are for the red, and when translated into a base ten number, they equal 255, which is the maximum value for any one color.

## How are they used?

It is in this context that bitwise operations become truly useful. Many simple image processing can be done by simple use of bitwise operators. To extract one of the color channels (such as red, green, or blue) from an image, one can use a bitmask and the bitwise "and" operator. In C++, which is the language these tutorials focus on, the bitwise and is a single "&." When two bits are compared using the and operator, a one is returned if both are one, and a zero is returned in all other cases. In the same fashion, we can and sets of bits. If we use the bitmask 111111110000000000000000, which represents red, and and it with each of the other pixels in the image one at a time, only the red values will remain. Presto: we've just separated the image into its red component!!!

## So, what are some other bitwise operators?

Well, there are a few more... actually, there are a lot more. Related to the and operator, there is the or operator, which returns one if either or both of the bits it is comparing are one and zero otherwise, and the xor (exclusive-or) operator, which is similar to or except that it returns a zero if both arguments are one. There is also the not operator, which inverts the bit and yields a zero if it is one and a one if it is zero.
There are also so-called bitshift operators, which move the bits around inside the number. For example, a left bitshift "rotates" the number so that the leftmost bit becomes the rightmost bit. A single left shift of the binary number 10011010, for example, yields 00110101. A right shift does the reverse. There are also variations in which the space to the right of the number is truncated (for a right shift), or zero-filled (in the case of a left shift). In C++, the bitshift operators are << for a left shift (zero filling) and >> for a right shift (zero filling on the left).