Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - Operators
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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? 
VISUAL BASIC.NET

Operators
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-09-06

    Table of Contents:
  • Operators
  • Concatenation Operators
  • Logical and Bitwise Operators Continued
  • Assignment Operators

  • 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


    Operators - Logical and Bitwise Operators Continued


    (Page 3 of 4 )

     

    And

    The And operator performs a logical or bitwise conjunction on the two source operands. In logical operations, it returns True if and only if both operands evaluate to True. If either operand is False, then the result is False. The syntax is:

      result = expression1 And expression2

    For example, consider the following statement:

      If (x = 5) And (y < 7) Then

    In this case, the code within the Then clause will be executed only if the value of x is 5 and the value of y is less than 7.

    As a bitwise operator, And returns 1 in a bit position if the compared bits in the same position in both expressions are 1, and it returns 0 in all other cases, as shown in the following table:

    Bit in expression1 0

    Bit in expression2 0

    Result 0

    0

    1

    0

    1

    0

    0

    1

    1

    1

    For example, the bitwise result of 15 And 179 is 3, as the following binary representation shows:

      00001111 And 10110011 -> 00000011

    AndAlso

    The AndAlso operator works exactly like the logical And operator, but short-circuiting is enabled. If the first operand evaluates to False, the second operand is not evaluated at all, even if that expression includes function calls. Operands are evaluated from left to right. AndAlso does not perform bitwise operations.

    Or

    The Or operator performs a logical or bitwise disjunction on the two source operands. In logical operations, it returns True if either of the operands evaluates to True. If both operands are False, then the result is False. The syntax is:

      result = expression1 Or expression2

    For example, consider the following statement:

      If (x = 5) Or (y < 7) Then

    In this case, the code within the Then clause will be executed if either the value of x is 5 or the value of y is less than 7.

    As a bitwise operator, Or returns 1 in a bit position if either of the compared bits in the same position in the source expressions are 1, and it returns 0 in all other cases, as shown in the following table:

    Bit in expression1 0

    Bit in expression2 0

    Result 0

    0

    1

    1

    1

    0

    1

    1

    1

    1

    For example, the bitwise result of 15Or179 is 191, as the following binary representation shows:

      00001111 Or 10110011 -> 10111111

    OrElse

    The OrElse operator works exactly like the logical Or operator, but short-circuiting is enabled. If the first operand evaluates to True, the second operand is not evaluated at all, even if that expression includes function calls. Operands are evaluated from left to right. OrElse does not perform bitwise operations.

    Not

    The Not operator performs a logical or bitwise negation on a single expression. In logical operations, it returns True if the operand is False, and False if the operand is True. The syntax is:

      result = Not expression1

    For example, consider the following statement:

      If Not IsNumeric(x) Then

    In this example, the code within the Then clause will be executed if IsNumeric returns False, indicating that x is not a value capable of being represented by a number.

    As a bitwise operator, Not simply toggles the value of each bit in the source expression between 0 and 1, as shown in the following table:

    Bit in expression1

    Result

    0

    1

    1

    0

    For example, the bitwise result of Not 16 is 239, as the following binary representation shows:

      Not 00010000 -> 11101111

    Xor

    The Xor (an abbreviation for "eXclusive OR") operator performs a logical or bitwise exclusion on the two source operands. In logical operations, it returns True if and only if the two expressions have different truth values. If both expressions are True, or both are False, this operator returns False. If one of the operands is True but the other False, then Xor returns True. The syntax is:

      result = expression1 Xor expression2

    As a bitwise operator, Xor returns 1 in a bit position if the compared bits are different from each other, and it returns 0 if they are the same, as shown in the following table:

    Bit in expression1 0

    Bit in expression2 0

    Result 0

    0

    1

    1

    1

    0

    1

    1

    1

    0

    For example, the result of 15 Xor 179 is 188, as the following binary representation shows:

      00001111 Xor 10110011 -> 10111100


    Eqv and Imp

    Eqv and Imp, two logical and bitwise operators present in VB 6, have been removed from .NET implementations of Visual Basic. Eqv can be replaced with the = (equal to) comparison operator. The expression:

      expression1 Eqv expression2

    is the same as the logical comparison:

      expression1 = expression2

    Imp can be replaced with a logical expression using the Not and Or operators. For example:

      expression1 Imp expression2

    can also be expressed as:

      (Not expression1) Or expression2

    If you need more precise replacements using bitwise calculations, see the "Logical and Bitwise Operators"  section in Appendix D.


    << (Shift Left)

    New in 2003. The << (shift left) operator performs a left shift of the bits in the first operand by the number of bits specified in the second operand. All bits shifted off the left are lost. All bits newly vacated on the right are filled with zeros.

    The number of bits you can shift is limited by the number of possible bits in the first operand. Any excess number of shift positions will be ignored. This operator never throws an overflow exception. The syntax is:

      result = source << bits

    For example, the bitwise result of 15 << 5 is 224, as the following binary representation shows:

      00001111 << 5 -> 11100000

     >> (Shift Right)

    New in 2003. The >> (shift right) operator performs a right shift of the bits in the first operand by the number of bits specified in the second operand. All bits shifted off the right are lost. All bits newly vacated on the left are filled with the bit value of the leftmost bit position before shifting. When shifting unsigned data values (Byte , UShort, UInteger, ULong), the newly vacated bits on the left are filled with zero (0).

    The number of bits you can shift is limited by the number of possible bits in the first operand. Any excess number of shift positions will be ignored. This operator never throws an overflow exception. The syntax is:

      result = source >> bits

    For example, the bitwise result of 12 >> 1 is 6, as the following binary representation shows:

      00001100 >> 1 -> 00000110

    More Visual Basic.NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Visual Basic 2005 in a Nutshell, Third...
     

    Buy this book now. This article is excerpted from chapter five of Visual Basic 2005 in a Nutshell, Third Edition, written by Tim Patrick, Steven Roman, Ph.D., Ron Petrusha and Paul Lomax (O'Reilly; ISBN: 059610152X). Check it out today at your favorite bookstore. Buy this book now.

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek