C#
  Home arrow C# arrow Page 4 - Numbers
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
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#

Numbers
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 19
    2004-06-29

    Table of Contents:
  • Numbers
  • Determining Approximate Equality Between a Fraction and Floating-Point Value
  • Converting Degrees to Radians and Converting Radians to Degrees
  • Using the Bitwise Complement Operator with Various Data Types
  • Test for an Even or Odd Value
  • Converting a Number in Another Base to Base10 and Determining Whether a String Is a Valid Number
  • Determining Whether a String Is a Valid Number
  • Rounding a Floating-Point Value and Different Rounding Algorithms Problem
  • Converting Celsius to Fahrenheit and
  • Safely Performing a Narrowing Numeric Cast
  • Discussion
  • Finding the Length of Any Three Sides of a Right Triangle and Finding the Angles of a Right Triangle Problem

  • 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


    Numbers - Using the Bitwise Complement Operator with Various Data Types


    (Page 4 of 12 )

    1.4 Using the Bitwise Complement Operator with Various Data Types

    Problem

    The bitwise complement operator (~) is overloaded to work directly with int, uint, long, ulong, and enumeration data types consisting of the underlying types int, uint, long, and ulong. However, you need to perform a different bitwise complement operation on a data type.

    Solution

    You must cast the resultant value of the bitwise operation to the type you wish to work. The following code demonstrates this technique with the byte data type:

    byte y = 1;
    byte result = (byte)~y;

    The value assigned to result is 254.

    Discussion

    The following code shows incorrect use of the bitwise complement operator on the byte data type:

    byte y = 1;
    Console.WriteLine("~y = " + ~y);

    This code outputs the following surprising value:

    -2

    Clearly, the result from performing the bitwise complement of the byte variable is incorrect; it should be 254. In fact, byte is an unsigned data type, so it cannot be equal to a negative number. If we rewrite the code as follows:

    byte y = 1;
    byte result = ~y;

    we get a compile-time error: “Cannot implicitly convert type ‘int’ to ‘byte.’” This error message gives some insight into why this operation does not work as expected. To fix this problem, we must explicitly cast this value to a byte before we assign it to the result variable, as shown here:

    byte y = 1;
    byte result = (byte)~y;

    This cast is required because the bitwise operators are only overloaded to operate on six specific data types: int, uint, long, ulong, bool, and enumeration data types. When one of the bitwise operators is used on another data type, that data type is converted to the next closest data type of the six supported data types. Therefore, a byte data type is converted to an int before the bitwise complement operator is evaluated:

    0x01 // byte y = 1;
    0xFFFFFFFE // The value 01h is converted to an int and its
    // bitwise complement is taken
    0xFE // The resultant int value is cast to its original byte data type

    Notice that the int data type is a signed data type, unlike the byte data type. This is why we receive –2 for a result instead of the expected value 254. This conversion of the byte data type to its nearest equivalent is called numeric promotion. Numeric promotion also comes into play when you use differing data types with binary operators, including the bitwise binary operators.

    Note: Numeric promotion is discussed in detail in the C# Language Specification document in section 7.2.6 (this document is found in the directory \Microsoft Visual Studio .NET 2003\Vc7\1033 belowthe .NET 2003 installation directory). Understanding hownumeric promotion works is essential when using operators on differing data types and when using operators with a data type that it is not overloaded to handle. Knowing this can save you hours of debugging time.  

    Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

    Visit the O'Reilly Network http://www.oreillynet.com for more online content.

    More C# Articles
    More By O'Reilly Media


     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





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