Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Compunet Corporation1 Programming with Visual Basic.NET Arithmetic, Logical & Bitwise Operators Week # 3 Tariq Ibn Aziz."— Presentation transcript:

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

2 Feb 23, 2007Dammam Community College2 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

3 Feb 23, 2007Dammam Community College3 Operators Arithmetic ^, –, *, /, \, Mod, +, = Assignment =, ^=, *=, /=, \=, +=, -=, &= Comparison =, <>,, =, Like, Is Concatenation&, + Logical/bitwise operations Not, And, Or, Xor, AndAlso, OrElse

4 Feb 23, 2007Dammam Community College4 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

5 Feb 23, 2007Dammam Community College5 Arithmetic Operators Exercise Is the following valid expression? ((10) + (((5)))/9)

6 Feb 23, 2007Dammam Community College6 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

7 Feb 23, 2007Dammam Community College7 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

8 Feb 23, 2007Dammam Community College8 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)

9 Feb 23, 2007Dammam Community College9 Operator Precedence and Associativity Exponentiation^ Unary negation+, - Multiplicative*, / Integer division\ ModulusMod Additive+, - Concatenation& Shift > Relational=, <>,, =, Like, Is, TypeOf...Is Logical NOTNot Logical ANDAnd, AndAlso Logical OROr, OrElse Logical XORXor

10 Feb 23, 2007Dammam Community College10 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.

11 Feb 23, 2007Dammam Community College11 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

12 Feb 23, 2007Dammam Community College12 Associativity Quiz Can you think of any right associative operators?

13 Feb 23, 2007Dammam Community College13 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. 1.b * b – 4 * a * c 2.c – b / a * a 3.a * (b + c) 4.2^3^2

14 Feb 23, 2007Dammam Community College14 The Bitwise Operators OperatorMeaning >> 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

15 Feb 23, 2007Dammam Community College15 The Bitwise Operators ' Low-order bits of 12 are 0000 1100 Dim Pattern As Integer = 12 Dim LResult, RResult As Integer ' Left shift of 3 bits produces value of 96 LResult = Pattern << 3 ' Right shift of 2 bits produces value of 3 RResult = Pattern >> 2

16 Feb 23, 2007Dammam Community College16 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 As Integer = 10 Dim B As Integer = 8 Dim C As Integer = 6 Dim myCheck As Boolean myCheck = A > B And B > C ' Returns True. myCheck = B > A And B > C ' Returns False.

17 Feb 23, 2007Dammam Community College17 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 As Integer = 10 Dim B As Integer = 8 Dim C As Integer = 6 Dim myCheck As Integer myCheck = (A And B) ' Returns 8. myCheck = (A And C) ' Returns 2. myCheck = (B And C) ' Returns 0.

18 Feb 23, 2007Dammam Community College18 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 As Integer = 10 Dim B As Integer = 8 Dim C As Integer = 6 Dim myCheck As Integer myCheck = (Not A) ' Returns -11. myCheck = (Not B) ' Returns -9. myCheck = (Not C) ' Returns -7.

19 Feb 23, 2007Dammam Community College19 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 As Integer = 10 Dim B As Integer = 8 Dim C As Integer = 6 Dim myCheck As Boolean myCheck = Not(A > B) ' Returns False. myCheck = Not(B > A) ' Returns True.

20 Feb 23, 2007Dammam Community College20 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 As Integer = 10 Dim B As Integer = 8 Dim C As Integer = 6 Dim myCheck As Boolean myCheck = A > B Or B > C ' Returns True. myCheck = B > A Or B > C ' Returns True. myCheck = B > A Or C > B ' Returns False.

21 Feb 23, 2007Dammam Community College21 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 As Integer = 10 Dim B As Integer = 8 Dim C As Integer = 6 Dim myCheck As Integer myCheck = (A Or B) ' Returns 10. myCheck = (A Or C) ' Returns 14. myCheck = (B Or C) ' Returns 14.

22 Feb 23, 2007Dammam Community College22 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 As Integer = 10 Dim B As Integer = 8 Dim C As Integer = 6 Dim myCheck As Boolean myCheck = A > B Xor B > C ' Returns False. myCheck = B > A Xor B > C ' Returns True. myCheck = B > A Xor C > B ' Returns False.

23 Feb 23, 2007Dammam Community College23 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 As Integer = 10 Dim B As Integer = 8 Dim C As Integer = 6 Dim myCheck As Integer myCheck = (A Xor B) ' Returns 2. myCheck = (A Xor C) ' Returns 12. myCheck = (B Xor C) ' Returns 14.

24 Feb 23, 2007Dammam Community College24 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 =,, <>, =, 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

25 Feb 23, 2007Dammam Community College25 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.

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

27 Feb 23, 2007Dammam Community College27 Safe Type conversion ByteInt16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal Int16Int32, Int64, Single, Double, Decimal CharUInt16, UInt32, Int32, UInt64, Int64, Single, Double, Décimal Int32Int64, Double, Decimal Int64Decimal SingleDouble

28 Feb 23, 2007Dammam Community College28 May Lose Precision in Type conversion Int32Single UInt32Single Int64Single, Double UInt64Single, Double DecimalSingle, Double

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


Download ppt "Compunet Corporation1 Programming with Visual Basic.NET Arithmetic, Logical & Bitwise Operators Week # 3 Tariq Ibn Aziz."

Similar presentations


Ads by Google