1 / 29

Programming with Visual Basic .NET

Programming with Visual Basic .NET. Arithmetic, Logical & Bitwise Operators Week # 3 Tariq Ibn Aziz. Objectives. Arithmetic Operators & Compound Assignment Categories of Operators Operator Precedence and Associativity User-defined Data Types (UDT) The Bitwise Operators

justinbaker
Download Presentation

Programming with Visual Basic .NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with Visual Basic .NET Arithmetic, Logical & Bitwise Operators Week # 3 Tariq Ibn Aziz Compunet Corporation

  2. Objectives • Arithmetic Operators & Compound Assignment • Categories of Operators • Operator Precedence and Associativity • User-defined Data Types (UDT) • The Bitwise Operators • Logical AND, OR, XOR, NOT operator • Short-Circuiting Operators • Safe / May Loose Type conversion Dammam Community College

  3. Operators Dammam Community College

  4. Arithmetic Operators Module Module1 Sub Main() System.Console.WriteLine(5 / 2) ' 2.5 System.Console.WriteLine(5 \ 2) ' 2 System.Console.WriteLine(5 Mod 2) ' 1 System.Console.WriteLine(4 Mod 2) ' 0 System.Console.WriteLine(4 * 2) ' 8 System.Console.WriteLine(8 >> 1) ' 4 System.Console.WriteLine(1 << 3) ' 8 End Sub End Module Dammam Community College

  5. Arithmetic Operators • Exercise • Is the following valid expression? ((10)+(((5)))/9) Dammam Community College

  6. Compound Assignment • The following are shortcut operators: • +=, -=, *=, /=, \=, ^=, &= • Compound assignment operators are shorthand for simple assignment statements. For example, • x += 5 • is equivalent to • x = x + 5 Dammam Community College

  7. Practice • Evaluate each of the following expressions: • 20 / 6 • 20 \ 6 • 3 / 4 • Assume i and j are integers with values 2 and 5, respectively. • i *= j + 3 Dammam Community College

  8. Categories of Operators • Arithmetic and Concatenation Operators • Exponentiation (^) • Unary negation (–) • Multiplication and division (*, /) • Integer division (\) • Modulus arithmetic (Mod) • Addition and subtraction (+, –), string concatenation (+) • String concatenation (&) • Arithmetic bit shift (<<, >>) • Comparison Operators • All comparison operators (=, <>, <, <=, >, >=, Like, Is, TypeOf...Is) • Logical and Bitwise Operators • Negation (Not) • Conjunction (And, AndAlso) • Disjunction (Or, OrElse, Xor) Dammam Community College

  9. Operator Precedence and Associativity Dammam Community College

  10. Associativity • When an operand occurs between two operators with the same precedence, the associativity of the operators controls the order in which the operations are performed. • All binary operators are left-associative, meaning that operations are performed from left to right. Precedence and associativity can be controlled using parenthetical expressions. Dammam Community College

  11. Associativity • Left Associativity means that operations at the same precedence level are applied from left to right. Right Associativity means right to left. • Most binary operators are left associative. For example, 10 \ 5 \ 2 • Answer is 1, (not 5) since the left-most \ is done first. • What is the value of the following expression? 4 \ 3 \ 2 Dammam Community College

  12. Associativity Quiz • Can you think of any right associative operators? Dammam Community College

  13. Practice • Write the following expressions in their fully parenthesized form to show the order in which operations are performed in VB. If you need help, refer to the precedence table located on page 158 of your book. • b * b – 4 * a * c • c – b / a * a • a * (b + c) • 2^3^2 Dammam Community College

  14. The Bitwise Operators • Operator Meaning >> Shift right with sign extension << Shift left with zero fill • The shift operators have this general form: • value <<num • value >>num • Here, value is the value being shifted and num is the number of bit position to shift. e.g. 16 >> 2 yields 4 Dammam Community College

  15. The Bitwise Operators ' Low-order bits of 12 are 0000 1100 Dim Pattern AsInteger = 12 Dim LResult, RResult AsInteger ' Left shift of 3 bits produces value of 96 LResult = Pattern << 3 ' Right shift of 2 bits produces value of 3 RResult = Pattern >> 2 Dammam Community College

  16. The Logical AND Operators • This example uses the And operator to perform a logical conjunction on two expressions. The result is a Boolean value that represents whether the entire conjoined expression is true. Dim A AsInteger = 10 Dim B AsInteger = 8 Dim C AsInteger = 6 Dim myCheck AsBoolean myCheck = A > B And B > C ' Returns True. myCheck = B > A And B > C ' Returns False. Dammam Community College

  17. The Bitwise AND Operators • This example uses the And operator to perform logical conjunction of the individual bits of two numeric expressions. The bit in the result pattern is set if the corresponding bits in the operands are both set. Dim A AsInteger = 10 Dim B AsInteger = 8 Dim C AsInteger = 6 Dim myCheck AsInteger myCheck = (A And B) ' Returns 8. myCheck = (A And C) ' Returns 2. myCheck = (B And C) ' Returns 0. Dammam Community College

  18. The Bitwise NOT Operators • This example uses the Not operator to perform logical negation of the individual bits of two numeric expressions. The bit in the result pattern is set to the reverse of the corresponding bit in the operand pattern, including the sign bit. Dim A AsInteger = 10 Dim B AsInteger = 8 Dim C AsInteger = 6 Dim myCheck AsInteger myCheck = (Not A) ' Returns -11. myCheck = (Not B) ' Returns -9. myCheck = (Not C) ' Returns -7. Dammam Community College

  19. The Logical NOT Operators • This example uses the Not operator to perform logical negation on a Boolean expression. The result is a Boolean value representing whether the expression is false. That is, if the expression is false, the result of the Not operator is true. Dim A AsInteger = 10 Dim B AsInteger = 8 Dim C AsInteger = 6 Dim myCheck AsBoolean myCheck = Not(A > B) ' Returns False. myCheck = Not(B > A) ' Returns True. Dammam Community College

  20. The Logical OR Operators • This example uses the Or operator to perform logical disjunction on two expressions. The result is a Boolean value that represents whether either of the two expressions is true. Dim A AsInteger = 10 Dim B AsInteger = 8 Dim C AsInteger = 6 Dim myCheck AsBoolean myCheck = A > B Or B > C ' Returns True. myCheck = B > A Or B > C ' Returns True. myCheck = B > A Or C > B ' Returns False. Dammam Community College

  21. The Bitwise OR Operators • This example uses the Or operator to perform logical disjunction of the individual bits of two numeric expressions. The bit in the result pattern is set if either of the corresponding bits in the operands are set. Dim A AsInteger = 10 Dim B AsInteger = 8 Dim C AsInteger = 6 Dim myCheck AsInteger myCheck = (A Or B) ' Returns 10. myCheck = (A Or C) ' Returns 14. myCheck = (B Or C) ' Returns 14. Dammam Community College

  22. The Logical XOR Operators • This example uses the Xor operator to perform logical exclusion on two expressions. The result is a Boolean value representing whether only one of the expressions is true. Dim A AsInteger = 10 Dim B AsInteger = 8 Dim C AsInteger = 6 Dim myCheck AsBoolean myCheck = A > B Xor B > C ' Returns False. myCheck = B > A Xor B > C ' Returns True. myCheck = B > A Xor C > B ' Returns False. Dammam Community College

  23. The Bitwise XOR Operators • This example uses the Xor operator to perform logical exclusion of the individual bits of two numeric expressions. The bit in the result pattern is set if only one of the corresponding bits in the operands are set. Dim A AsInteger = 10 Dim B AsInteger = 8 Dim C AsInteger = 6 Dim myCheck AsInteger myCheck = (A Xor B) ' Returns 2. myCheck = (A Xor C) ' Returns 12. myCheck = (B Xor C) ' Returns 14. Dammam Community College

  24. Boolean Expressions • A Boolean expression is an expression that evaluates to a Boolean value If x = True then ' Compare x to Boolean value True. y = False ' Assign Boolean value to y. End If • Comparison operators, such as =, <, >, <>, <=, and >=, produce Boolean expressions 42 < 81 ' Evaluates to True. • Comparison expressions can be combined using logical operators to produce more complex Boolean expressions. x > y And x < 1000 Dammam Community College

  25. Short-Circuiting Operators • The logical operators AndAlso and OrElse exhibit behavior known as short-circuiting. A short-circuiting operator evaluates the left expression first. If 45 < 12 AndAlso MyFunction(3) = 81 Then ' Add code to continue execution. • In this example, the operator evaluates the left expression, 45 < 12. Since this expression evaluates to False, the entire logical expression evaluates to False. Dammam Community College

  26. Short-Circuiting Operators (Contd.) • By contrast, both sides of the logical operator are evaluated when the logical operators AND/ ORare used. Consider the following expression for example: If 45 < 12 And MyFunction(3) = 81 Then ' Add code to continue execution. Dammam Community College

  27. Safe Type conversion Dammam Community College

  28. May Lose Precision in Type conversion Dammam Community College

  29. Boolean operator Exercise • What is wrong in this Statement? K = K + 1 System.Console.WriteLine ( k ) • Is this expression true? NOT(10 = 9) Dammam Community College

More Related