C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.

Slides:



Advertisements
Similar presentations
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Advertisements

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.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
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.
Compunet Corporation1 Programming with Visual Basic.NET Arithmetic, Logical & Bitwise Operators Week # 3 Tariq Ibn Aziz.
Operations on data CHAPTER 4.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
C Operators, Operands, Expressions & Statements Hands-on, crash course with code examples 1/46 ©
4. Python - Basic Operators
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
OperatorstMyn1 Operators The sequence in which different operators in an expression are executed is determined by the precedence of the operators. Operators.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
2440: 211 Interactive Web Programming Expressions & Operators.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
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.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
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.
ISBN Chapter 7 Expressions and Assignment Statements.
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.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Operators & Expressions
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
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.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
CHAPTER 4 CS 3370 – C++ Expressions. Operators Unary -, *, ++, -- higher precedence than binary operators most associate right-to-left Binary most associate.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Operator Kinds of Operator Precedence of Operator Type Casting.
DEPARTMENT OF COMPUTER SCIENCE C Operators, Operands, Expressions & Statements 1/46 BCA I ST YR (SEMESTER I) PRESENTED BY SHAZIA SHAFI Dated:
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Operators & Expressions
CSE 220 – C Programming Expressions.
Operators and Expressions
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Rational Expressions. relational operators. logical operators
Selections Java.
University of Central Florida COP 3330 Object Oriented Programming
Operators and Expressions
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
All the Operators 22-Nov-18.
All the Operators 4-Dec-18.
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Associativity and Prescedence
Introduction to Programming – 4 Operators
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
Operator King Saud University
OPERATORS in C Programming
Presentation transcript:

C Operators

CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE

Conditional Operator (ternary) ?: The first operand/expression is evaluated, and its value determines whether the second or third operand/expression is evaluated: 1.If the value is true, the second operand/expression is evaluated. 2.If the value is false, the third operand/expression is evaluated. The result is the value of the second or third operand/expression. The syntax is: First operand ? second operand : third operand size != 0 ? size : 0 BACK TO INDEX

 The compound assignment operators consist of a binary operator and the simple assignment operator.  They perform the operation of the binary operator on both operands and store the result of that operation into the left operand.  The following table lists the simple and compound assignment operators and expression examples:

Simple Assignment Operator =  The simple assignment operator has the following form: lvalue = expr  The operator stores the value of the right operand expr in the object designated by the left operand lvalue.  The left operand must be a modifiable lvalue.  The type of an assignment operation is the type of the left operand. i = 5 + x; BACK TO INDEX

Compound Assignment Operator ExampleEquivalent expression identifier operator= entity represents identifier = identifier operator entity += X+=7X= X+7 -= X-=7X= X-7 *= X*=7X= X*7 /= X/=7X= X/7 %=X%=7X= X%7 BACK TO INDEX

Bitwise (complement) NOT Operators ~ The ~ (bitwise negation) operator yields the bitwise (one) complement of the operand. In the binary representation of the result, every bit has the opposite value of the same bit in the binary representation of the operand. The operand must have an integral type. The result has the same type as the operand but is not an lvalue (left value). The symbol used called tilde. Suppose byNum variable represents the decimal value 8. The 16-bit binary representation of byNum is: The expression ~byNum yields the following result (represented here as a 16-bit binary number):

Bitwise Shift Operators << Left shift operator, shift their first operand left ( << ) by the number of positions specified by the second operand. nbits << nshiftSize >> Right shift operator, shift their first operand right ( >> ) by the number of positions specified by the second operand. nbits >> nshiftSize  Both operands must be integral values. These operators perform the usual arithmetic conversions; the type of the result is the type of the left operand after conversion.  For leftward shifts, the vacated right bits are set to 0. For rightward shifts, the vacated left bits are filled based on the type of the first operand after conversion. If the type is unsigned, they are set to 0. Otherwise, they are filled with copies of the sign bit. For left-shift operators without overflow, the statement: expression1 << expression2  is equivalent to multiplication by 2 expression2. For right-shift operators: expression1 >> expression2  is equivalent to division by 2 expression2 if expression1 is unsigned or has a nonnegative value.  The result of a shift operation is undefined if the second operand is negative, or if the right operand is greater than or equal to the width in bits of the promoted left operand.  Since the conversions performed by the shift operators do not provide for overflow or underflow conditions, information may be lost if the result of a shift operation cannot be represented in the type of the first operand after conversion.

Bitwise AND Operator &  The & (bitwise AND ) operator compares each bit of its first operand to the corresponding bit of the second operand. If both bits are 1's, the corresponding bit of the result is set to 1. Otherwise, it sets the corresponding result bit to 0.  The bitwise AND ( & ) should not be confused with the logical AND. ( && ) operator. For example: 1 & 4 evaluates to 0 ( 0001 & 0100 = 0000 ) while 1 && 4 evaluates to true [ True && True = True ]

Bitwise XOR Operator ^  The bitwise exclusive OR operator ( the ^ symbol ) compares each bit of its first operand to the corresponding bit of the second operand. If both bits are 1 's or both bits are 0 's, the corresponding bit of the result is set to 0. Otherwise, it sets the corresponding result bit to 1.

Bitwise (Inclusive) OR Operator |  The | (bitwise inclusive OR ) operator compares the values (in binary format) of each operand and yields a value whose bit pattern shows which bits in either of the operands has the value 1. If both of the bits are 0, the result of that bit is 0 ; otherwise, the result is 1.  The bitwise OR ( | ) should not be confused with the logical OR ( || ) operator. For example: 1 | 4 evaluates to 5 ( 0001 | 0100 = 0101 ) while 1 || 4 ( True || True = True ) evaluates to true BACK TO INDEX

Precedence and Associativity of C Operators SymbolType of OperationAssociativity [ ] ( ). –> postfix ++ and postfix –– ExpressionLeft to right prefix ++ and prefix –– sizeof & * + – ~ ! UnaryRight to left typecasts UnaryRight to left * / % MultiplicativeLeft to right + – AdditiveLeft to right > Bitwise shiftLeft to right = RelationalLeft to right == != EqualityLeft to right & Bitwise-ANDLeft to right ^ Bitwise-exclusive-ORLeft to right | Bitwise-inclusive-ORLeft to right && Logical-ANDLeft to right || Logical-ORLeft to right ? : Conditional-expressionRight to left = *= /= %= += –= >= &= ^= |= Simple and compound assignment Right to left, Sequential evaluationLeft to right BACK TO INDEX

BACK TO INDEX