Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.

Slides:



Advertisements
Similar presentations
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
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.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Simple Data Types Built-In and User Defined Chapter 10.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
 All C programs are made up of functions that perform operations on variables.  In this lecture we examine variables  Variables are the basic building.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Control statements Mostafa Abdallah
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Decision Statements, Short- Circuit Evaluation, Errors.
Controlling Program Flow with Decision Structures.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CMSC 202 Lesson 26 Miscellaneous Topics. Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a;
The Machine Model Memory
Chapter 3 Control Statements
Data types Data types Basic types
Chapter 4 C Program Control Part I
Chap. 2. Types, Operators, and Expressions
Week 3 - Friday CS222.
Selections Java.
Bit Operations Horton pp
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
C Basics.
Introduction to C Programming
Introduction to Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Chapter 7 Additional Control Structures
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
OPERATORS in C Programming
Bit Operations Horton pp
Presentation transcript:

Windows Programming Lecture 06

Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal numbers – those do not stores decimal numbers Known as Integral data types For example, integer, char, long integer etc.

Bitwise Operators An operator that manipulates individual bits. The operators that most people are familiar with, such as the addition operator (+), work with bytes or groups of bytes. Occasionally, however, programmers need to manipulate the bits within a byte. The C programming language supports the following bitwise operators: >> Shifts bits right << Shifts bits left & Does an AND compare on two groups of bits | Does an OR compare on two groups of bits ^ Does an XOR compare on two groups of bits ~ Complements a group of bits

Bitwise Shift Operators There are two bitwise shift operators, namely shift left and shift right. In C, they are represented by the > operators, respectively. These operations are very simple, and do exactly what they say: shift bits to the left or to the right.

Bitwise Left Shift (<<) Operator Bitwise Left-Shift is useful when to want to MULTIPLY an integer (not floating point numbers) by a power of 2. The operator, like many others, takes 2 operands like this: a << b This expression returns the value of a multiplied by 2 to the power of b.

Right Most Bit Left Most Bit

Bitwise Left Shift (<<) Operator Why is it called a left shift? Take the binary representation of a, and add b number of zeros to the right, consequently "shifting" all the bits b places to the left. Example: 4 << 2. 4 is 100 in binary. Adding 2 zeros to the end gives 10000, which is 16, i.e. 4*2*2 = 4*4 = 16. What is 4 << 3 ? Simply add 3 zeros to get , which is 4*23 = 4*8 = 32.

Bitwise Right Shift (>>) Operator Bitwise Right-Shift does the opposite, and takes away bits on the right. Suppose we had: a >> b This expression returns the value of a divided by 2 to the power of b. Example: 8 >> 2. 8 is 1000 in binary. Performing a right shift of 2 involves knocking the last 2 bits off: 1000, which leaves us with 10, i.e >> 2 is the as doing 8/2*2 = 8/4 = 2. But what happens if we had a left operand that's not a power of 2? Let's try 9 >> 2. 9 is Now take off the last 2 bits, leaving us with 10, which is 2. But this does make sense, since 9/4 = 2.25, which rounds down to 2.

Right Most Bit Left Most Bit

Examples

Bitwise shift operations can only applied to Integral data types

Bitwise AND, Bitwise Inclusive OR and Bitwise Exclusive OR can only use Integral operands

AND, OR, NOT There are three major bitwise operators that can be used to combine two numbers: AND, OR, and XOR. When I say that an operator is bitwise, it means that the operation is actually applied separately to each bit of the two values being combined. For example, if x = y AND z, that means that bit 0 of x is actually bit 0 of y ANDed with bit 0 of z. Make sense?

Bitwise AND Operator There are two kinds of AND operators in the C language: the logical AND, and the bitwise AND. The former is represented by the && operator, and the latter uses the & operator. The bitwise AND works very much the same way, except that it works with two bits instead of two expressions. The bitwise AND operation returns 1 if and only if both of its operands are equal to 1. In other words, we have the following truth table for the bitwise AND.

Bitwise AND Operator 0 AND 0 = 0 0 AND 1 = 0 x AND 0 = 0 1 AND 0 = 0 x AND 1 = x 1 AND 1 = 1

Bitwise OR Operator Just as with the AND operation, there are two different types of OR in the C language. The logical OR uses the || operator, and the bitwise OR uses the | operator. The bitwise OR is very similar, in that it returns 0 if and only if both of its operands are 0.

Bitwise OR Operator 0 OR 0 = 0 0 OR 1 = 1 x OR 0 = x 1 OR 0 = 1 x OR 1 = 1 1 OR 1 = 1

XOR Operator The XOR is a little strange because there is no logical equivalent for it in C, even though many languages include one. The XOR operation is symbolized by the ^ character in C. The term XOR stands for "exclusive OR“, and means "one or the other, but not both." In other words, XOR returns 1 if and only if exactly one of its operands is 1. If both operands are 0, or both are 1, then XOR returns 0.

XOR Operator 0 XOR 0 = 0 0 XOR 1 = 1 x XOR 0 = x 1 XOR 0 = 1x XOR 1 = ~x 1 XOR 1 = 0

Examples

Application of Bitwise Operators Bitwise operators have two main applications. The first is using them to combine several values into a single variable. Suppose you have a series of flag variables which will always have only one of two values: 0 or 1 (this could also be true or false). The smallest unit of memory you can allocate to a variable is a byte, which is eight bits. But why assign each of your flags eight bits, when each one only needs one bit? Using bitwise operators allows you to combine data in this way.

typedef typedef unsigned long DWORD typedef int BOOL typedef unsigned char BYTE typedef unsigned short WORD typedef char CHAR typedef CHAR *LPSTR typedef const CHAR *LPCSTR

typedef unsigned long DWORD; 4 bytes 32 bits DWORD Single Word =16 bits Double Word = 32 bits

typedef unsigned long ULONG; typedef ULONG *PULONG; typedef unsigned short USHORT; USHORT *PUSHORT; typedef

Typecasting char ch; int i; … … … some code here … … … i = ch;// no problem ch = i;// compiler warning

Typecasting Typecasting is making a variable of one type, act like another type for one single application. There are two types of typecasting. Implicit typecasting (coercion) Explicit typecasting Implicit type casting(coercion) is further divided in two types. Promotion Demotion

Promotion Typecasting

Demotion Typecasting

Macros #define LOWORD(l) ( (WORD)(l) ) #define HIWORD(l) ( (WORD)( ( (DWORD)(l) >> 16)&0xFFFF) ) int b=2623; // 0xA3F HIWORD(2623)// call to macro #define LOBYTE(w) ( (BYTE)(w) ) #define HIBYTE(w) ( (BYTE)( ( (WORD)(w) >> 8) & 0xFF) )

assertions In C, assertions are implemented with the standard assert macro. The argument to assert must be true when the macro is executed, otherwise the program aborts and prints an error message. For example, the assertion assert( size <= LIMIT ); will abort the program and print an error message like this: Assertion violation: file tripe.c, line 34: size <= LIMIT if size is greater than LIMIT.

Turning assertions off By default, ANSI C compilers generate code to check assertions at run-time. Assertion-checking can be turned off by defining the NDEBUG flag to your compiler.

Turning assertions off #ifdef NDEBUG #define assert(exp) ((void)0) #else // original definition of assert... #endif

The switch and case keywords The switch-case statement is a multi-way decision statement. Unlike the multiple decision statement that can be created using if- else, the switch statement evaluates the conditional expression and tests it against numerous constant values. The branch corresponding to the value that the expression matches is taken during execution. The value of the expressions in a switch-case statement must be an (integer, char, short, long), etc. Float and double are not allowed.

The switch and case keywords The syntax is : switch( expression ) { case constant-expression1: statements1; case constant-expression2: statements2; case constant-expression3: statements3; default : statements4; }

The switch and case keywords The case statements and the default statement can occur in any order in the switch statement. The default clause is an optional clause that is matched if none of the constants in the case statements can be matched. Multiple case labels together are possible. In the 'C' switch statement, execution continues on into the next case clause if it is not explicitly specified that the execution should exit the switch statement.