C++
  Home arrow C++ arrow Page 6 - More on Handling Basic Data Types
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

More on Handling Basic Data Types
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2005-03-02

    Table of Contents:
  • More on Handling Basic Data Types
  • Try It Out: Explicit Casting
  • Finding Out About Types
  • Try It Out: Finding the Sizes of Data Types
  • Using the Bitwise AND
  • Using the Bitwise Exclusive OR
  • Try It Out: Using the Bitwise Operators
  • More on Output Manipulators
  • Enumerated Data Types
  • Try It Out: Enumerated Data Types
  • The Lifetime of a Variable
  • Try It Out: The Scope Resolution Operator
  • Declaring External Variables

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More C++ Articles
    More By Apress Publishing


     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT