Home arrow C++ arrow Page 6 - More on Handling Basic Data Types
C++

More on Handling Basic Data Types


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).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 10
March 02, 2005
TABLE OF CONTENTS:
  1. · More on Handling Basic Data Types
  2. · Try It Out: Explicit Casting
  3. · Finding Out About Types
  4. · Try It Out: Finding the Sizes of Data Types
  5. · Using the Bitwise AND
  6. · Using the Bitwise Exclusive OR
  7. · Try It Out: Using the Bitwise Operators
  8. · More on Output Manipulators
  9. · Enumerated Data Types
  10. · Try It Out: Enumerated Data Types
  11. · The Lifetime of a Variable
  12. · Try It Out: The Scope Resolution Operator
  13. · Declaring External Variables

print this article
SEARCH DEVARTICLES

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:


draw_color           1111 1111 0000 0000 0000 0000
mask                 0000 0000 1111 1111 1111 1111
draw_color ^ mask    1111 1111 1111 1111 1111 1111


Clearly, you’ve changed the value of draw_color from red to white. Executing the same statement again will flip the color back to red:

 draw_color ^= mask;      // Switch the drawing color

This works as follows:


draw_color                 1111 1111 1111 1111 1111 1111
mask                       0000 0000 1111 1111 1111 1111
draw_color ^ mask          1111 1111 0000 0000 0000 0000

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.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials