Have you ever wanted to learn how basic types of C++ variables interact in complex situations? Ivor Horton explains this, and also describes some interesting features of C++. This article is from chapter 3 of Ivor Horton's Beginning ANSC C++ The Complete Language (Apress, 2004; ISBN 1590592271).
More on Handling Basic Data Types - Using the Bitwise Exclusive OR (Page 6 of 13 )
The bitwise exclusive OR operator is used much less frequently than the & and | operators, and there are few common examples of its use. An important application, though, arises in the context of graphics programming. One way of creating the illusion of motion on the screen is to draw an object, erase it, and then redraw it in a new position. This process needs to be repeated very rapidly if you are to get smooth animation, and the erasing is a critical part of it. You don’t want to erase and redraw the whole screen, as this is time consuming and the screen will flash. Ideally, you want to erase only the object or objects onscreen that you’re moving. You can do this and get reasonably smooth animation by using what is called exclusive OR mode.
Exclusive OR mode is based on the idea that once you’ve drawn an object on the screen in a given color, it will then disappear if you redraw it in the background color. This is illustrated by the sequence in Figure 3-3.
Figure 3-3.Drawing in exclusive OR mode
When you draw an object on the screen in exclusive OR mode, the color automatically alternates between the color you’ve selected for the object and the background color each time you draw the object. The key to achieving this is the use of the bitwise exclusive OR operator to alternate the colors rapidly and automatically. It uses a characteristic of the exclusive OR operation, which is that if you choose your values suitably, you can arrange to flip between two different values by repeated exclusive-OR operations. That sounds complicated, so let’s see how it works by looking at a specific example.
Suppose you want to alternate between a foreground color (you’ll use red), and a background color (white). As I noted earlier, color is often represented by three 8-bit values, corresponding to the intensities for each of red, blue, and green, and stored in a single 4-byte integer. By altering the proportions of red, blue, and green in a color, you can get around 16 million different colors in the range from white to black and everything in between. A bright red would be 0xFF0000, where the red component is set to its maximum and the intensities of the other two components for green and blue are zero. In the same scheme, green would be 0xFF00 and blue would be 0xFF. White has equal, maximum components of red, blue, and green, so it would be 0xFFFFFF.
You can therefore define variables representing red and white with the statements
unsigned long red = 0XFF0000UL; // Color red unsigned long white = 0XFFFFFFUL; // Color white – RGB all maximum
Next, you’ll create a mask that you can use to switch the color back and forth between red and white. You’ll also initialize the variable containing the drawing color to red:
unsigned long mask = red ^ white; // Mask for switching colors unsigned long draw_color = red; // Drawing color
The variable mask is initialized to the bitwise exclusive OR of the colors that you want to alternate, so it will be
red 1111 1111 0000 0000 0000 0000 white 1111 1111 1111 1111 1111 1111 mask (which is red ^ white) 0000 0000 1111 1111 1111 1111
If you exclusive-OR mask with red you’ll get white, and if you exclusive-OR mask with white you’ll get red. This is a very useful result. This means that having drawn an object using the color in draw_color, whichever it is, you can switch to the other color with the statement
draw_color ^= mask; // Switch the drawing color
The effect of this when draw_color contains red is as follows:
As you can see, draw_color is back to the value of red again. This technique will work with any two colors, although of course it has nothing to do with colors in particular at all—you can use it to alternate between any pair of integer values.
This article is excerpted from Beginning ANSI C++ The Complete Language by Ivor Horton (Apress, 2004; ISBN 1590592271). Check it out at your favorite bookstore today. Buy this book now.