OPERATORS IN C++ Arithmetic Operators: Relational Operators:

Similar documents
JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Logic in Computer Science: Logic Gates

Objective-C Tutorial

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Object Oriented Software Design

Positional Numbering System

Object Oriented Software Design

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Chapter 2: Elements of Java

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

APPENDIX B. Routers route based on the network number. The router that delivers the data packet to the correct destination host uses the host ID.

Binary Adders: Half Adders and Full Adders

NUMBER SYSTEMS. 1.1 Introduction

MEP Y9 Practice Book A

The string of digits in the binary number system represents the quantity

C++ Language Tutorial

Informatica e Sistemi in Tempo Reale

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Useful Number Systems

Chapter 4 Register Transfer and Microoperations. Section 4.1 Register Transfer Language

Computer Science 281 Binary and Hexadecimal Review

Classless Subnetting Explained

6.087 Lecture 2 January 12, 2010

Example of a Java program

Binary Numbers. Binary Octal Hexadecimal

Systems I: Computer Organization and Architecture

1. True or False? A voltage level in the range 0 to 2 volts is interpreted as a binary 1.

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Base Conversion written by Cathy Saxton

Sources: On the Web: Slides will be available on:

Chapter Binary, Octal, Decimal, and Hexadecimal Calculations

THE BINARY NUMBER SYSTEM

Paramedic Program Pre-Admission Mathematics Test Study Guide

Chapter 1: Digital Systems and Binary Numbers

1. Introductory Overview

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Number Systems and Radix Conversion

Subnetting Examples. There are three types of subnetting examples I will show in this document:

Today s topics. Digital Computers. More on binary. Binary Digits (Bits)

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Number Conversions Dr. Sarita Agarwal (Acharya Narendra Dev College,University of Delhi)

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

PROBLEMS (Cap. 4 - Istruzioni macchina)

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

CHAPTER 2. Logic. 1. Logic Definitions. Notation: Variables are used to represent propositions. The most common variables used are p, q, and r.

Chapter One Introduction to Programming

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Accuplacer Arithmetic Study Guide

Lecture 8: Binary Multiplication & Division

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Activity 6.7.4: IPv4 Address Subnetting Part 2

Classful Subnetting Explained

2.3 IPv4 Address Subnetting Part 2

Counters are sequential circuits which "count" through a specific state sequence.

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

COMBINATIONAL CIRCUITS

C Programming Tutorial

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

IP Subnetting and Related Topics A Tutorial by Chris Uriarte <chrisjur@cju.com> Updated April 2001

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

ETEC 2301 Programmable Logic Devices. Chapter 10 Counters. Shawnee State University Department of Industrial and Engineering Technologies

=

2.2: Bitwise Logical Operations

The C Programming Language course syllabus associate level

Playing with Numbers

MBA Jump Start Program

Lecture 2. Binary and Hexadecimal Numbers

3201 Computer Networks 2014/2015 Handout: Subnetting Question

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

Chapter 3: Writing C# Expressions

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

Chapter 2. Binary Values and Number Systems

Maths Workshop for Parents 2. Fractions and Algebra

Boolean Algebra Part 1

3 cups ¾ ½ ¼ 2 cups ¾ ½ ¼. 1 cup ¾ ½ ¼. 1 cup. 1 cup ¾ ½ ¼ ¾ ½ ¼. 1 cup. 1 cup ¾ ½ ¼ ¾ ½ ¼

Java Interview Questions and Answers

Math Review. for the Quantitative Reasoning Measure of the GRE revised General Test

CYCLIC REDUNDANCY CHECKS IN USB. Introduction. Two-minute mathematical background

SECTION C [short essay] [Not to exceed 120 words, Answer any SIX questions. Each question carries FOUR marks] 6 x 4=24 marks

Chapter 5 Instructor's Manual

Number and codes in digital systems

Teaching Pre-Algebra in PowerPoint

(Cat. No L3) Product Data

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Practical Programming, 2nd Edition

Section 1.4 Place Value Systems of Numeration in Other Bases

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

4.5 Finite Mathematical Systems

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Welcome to Math Video Lessons. Stanley Ocken. Department of Mathematics The City College of New York Fall 2013

X86-64 Architecture Guide

north seattle community college

Transcription:

OPERATORS IN C++ http://www.tutorialspoint.com/cplusplus/cpp_operators.htm Copyright tutorialspoint.com An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides the following types of operators: Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Misc Operators This chapter will examine the arithmetic, relational, logical, bitwise, assignment and other operators one by one. Arithmetic Operators: There are following arithmetic operators supported by C++ language: Assume variable A holds 10 and variable B holds 20, then: + Adds two s A + B will give 30 - Subtracts second from the first A - B will give -10 * Multiplies both s A * B will give 200 / Divides numerator by de-numerator B / A will give 2 % Modulus Operator and remainder of after an integer division ++ Increment operator, increases integer value by one -- Decrement operator, decreases integer value by one B % A will give 0 A++ will give 11 A-- will give 9 Relational Operators: There are following relational operators supported by C++ language Assume variable A holds 10 and variable B holds 20, then: == Checks if the values of two s are equal or not, if yes then condition A == B is not true.

!= Checks if the values of two s are equal or not, if values are not equal then condition > Checks if the value of left is greater than the value of right, if yes then condition < Checks if the value of left is less than the value of right, if yes then condition >= Checks if the value of left is greater than or equal to the value of right, if yes then condition becomes true. <= Checks if the value of left is less than or equal to the value of right, if yes then condition becomes true. A! = B is true. A > B is not true. A < B is true. A >= B is not true. A <= B is true. Logical Operators: There are following logical operators supported by C++ language Assume variable A holds 1 and variable B holds 0, then: && Called Logical AND operator. If both the s are non-zero, then condition A && B is false. Called Logical OR Operator. If any of the two s is non-zero, then condition! Called Logical NOT Operator. Use to reverses the logical state of its. If a condition is true, then Logical NOT operator will make false. A B is true.! A && B is true. Bitwise Operators: Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &,, and ^ are as follows: p q p & q p q p ^ q 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 1 1

Assume if A = 60; and B = 13; now in binary format they will be as follows: A = 0011 1100 B = 0000 1101 ----------------- A&B = 0000 1100 A B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011 The Bitwise operators supported by C++ language are listed in the following table. Assume variable A holds 60 and variable B holds 13, then: & Binary AND Operator copies a bit to the result if it exists in both s. A & B will give 12 which is 0000 1100 Binary OR Operator copies a bit if it exists in either. A B will give 61 which is 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one but not both. A B will give 49 which is 0011 0001 ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. << Binary Left Shift Operator. The left s value is moved left by the number of bits specified by the right. >> Binary Right Shift Operator. The left s value is moved right by the number of bits specified by the right. A will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. A << 2 will give 240 which is 1111 0000 A >> 2 will give 15 which is 0000 1111 Assignment Operators: There are following assignment operators supported by C++ language: = Simple assignment operator, Assigns values from right side s to left side += Add AND assignment operator, It adds right to the left and assign the result to left -= Subtract AND assignment operator, It subtracts right from the left C = A + B will assign value of A + B into C C += A is equivalent to C = C + A C -= A is equivalent to C = C - A

and assign the result to left *= Multiply AND assignment operator, It multiplies right with the left and assign the result to left /= Divide AND assignment operator, It divides left with the right and assign the result to left %= Modulus AND assignment operator, It takes modulus using two s and assign the result to left C *= A is equivalent to C = C * A C /= A is equivalent to C = C / A C %= A is equivalent to C = C % A <<= Left shift AND assignment operator C <<= 2 is same as C = C << 2 >>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2 &= Bitwise AND assignment operator C &= 2 is same as C = C & 2 ^= bitwise exclusive OR and assignment operator = bitwise inclusive OR and assignment operator C ^= 2 is same as C = C ^ 2 C = 2 is same as C = C 2 Misc Operators There are few other operators supported by C++ Language. Operator sizeof Description sizeof operator returns the size of a variable. For example, sizeofa, where a is integer, will return 4. Condition? X : Y Conditional operator. If Condition is true? then it returns value X : otherwise value Y, Comma operator causes a sequence of operations to be performed. The value of the entire comma expression is the value of the last expression of the comma-separated list.. dot and -> arrow Member operators are used to reference individual members of classes, structures, and unions. Cast & Casting operators convert one data type to another. For example, int2.2000 would return 2. Pointer operator & returns the address of an variable. For example &a; will give actual address of the variable. * Pointer operator * is pointer to a variable. For example *var; will pointer to a variable var. Operators Precedence in C++: Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:

For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7. Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first. Category Operator Associativity Postfix [] ->. ++ - - Left to right Unary + -! ~ ++ - - type* & sizeof Right to left Multiplicative * / % Left to right Additive + - Left to right Shift << >> Left to right Relational < <= > >= Left to right Equality ==!= Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR Left to right Logical AND && Left to right Logical OR Left to right Conditional?: Right to left Assignment = += -= *= /= %=>>= <<= &= ^= = Right to left Comma, Left to right Loading [MathJax]/jax/output/HTML-CSS/jax.js