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

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
The number of calories burned per hour by cycling, jogging and swimming are 200, 475 and 275 respectively. A person loses 1pound of weight for each 3500.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
CPS120: Introduction to Computer Science Operations Lecture 9.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
Boolean Logic Logical Operators Comparison Operators Truth tables.
1 Operators and Expressions. Expressions Combination of Operators and Operands Example 2 * y + 5 Operands Operators.
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Module 5 JavaScript Operators. CS346 Javascript-52 Examples  JS-5 Examples.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
Doing math In java.
OperatorstMyn1 Operators An operator is something that you feed with one or more values (or expressions, in programming jargon) which yields another value.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Rational Expressions relational operators logical operators order of precedence.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Operators and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Rational Expressions. relational operators. logical operators
University of Central Florida COP 3330 Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Control Structures: Part 2
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Lecture 3 Expressions Richard Gesick.
All the Operators 22-Nov-18.
Relational Operators Operator Meaning < Less than > Greater than
With Assignment Operator
All the Operators 4-Dec-18.
C Operators, Operands, Expressions & Statements
Expressions.
Data Types and Expressions
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
OPERATORS in C Programming
Data Types and Expressions
OPERATORS in C Programming
Data Types and Expressions
Presentation transcript:

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

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

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

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

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

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

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

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)

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

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.

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

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

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

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

Feb 23, 2007Dammam Community College15 The Bitwise Operators ' Low-order bits of 12 are 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

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.

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.

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.

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.

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.

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.

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.

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.

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

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.

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.

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

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

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)