Enumerations.

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

C Programming Lecture 23 Enumeration Types Structures.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
Senem Kumova Metin CHAPTER 7 Bitwise Operators and Enumeration Types.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.
1 Bitwise Operators. 2 Bitwise Operators (integers) Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement"
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to use the bitwise logical operators in programs ❏ To be able to use.
12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory  system-dependent places #include ::: system dependent.
Bitwise Operators in C. Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
Ch Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
Introduction to Computer Organization & Systems Topics: Command Line Bitwise operators COMP Spring 2014 C Part V.
Enumerated Types Mr. Dave Clausen La Cañada High School.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
Quick Summary C++/CLI Basics Data Types Controls Arrays In-class assignments.
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
10 주 강의 Bitwise operators and Enumeration types. 컴퓨터환경 8-bit bytes, 4-bytes words Two ’ s complement ASCII character codes.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
Introduction to ‘c’ language
5 Day Forecast Mon Tues Wed Thu Fri.
LESSON 06.
CSE 220 – C Programming Bitwise Operators.
The Machine Model Memory
Chapter 6 – Data Types CSCE 343.
Data types Data types Basic types
Structure, Unions, typedef and enumeration
Lecture 16: Introduction to Data Types
C Structures, Unions, Bit Manipulations and Enumerations
Enumeration Types and typedef
GANTT CHARTS Example Example Example Example text Tasks Example 1
Enumerations.
typedef typedef int Index; typedef char Letter; Index i; i = 17;
C: Primer and Advanced Topics
C Structures, Unions, Bit Manipulations and Enumerations
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Structures and Union.
Chapter 14 Bitwise Operators Objectives
MON TUE WED THU
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
ANNUAL CALENDAR HOLIDAYS JANUARY FEBRUARY MARCH APRIL MAY JUNE
HOLIDAYS ANNUAL CALENDAR JANUARY FEBRUARY MARCH APRIL MAY JUNE
Bits and Bytes Topics Representing information as bits
January Sun Mon Tue Wed Thu Fri Sat
Chapter 1: Introduction to Data Structures(8M)
HOLIDAYS ANNUAL CALENDAR JANUARY FEBRUARY MARCH APRIL MAY JUNE
2008 Calendar.
Structures and Union.
Sun Mon Tue Wed Thu Fri Sat
Comp Org & Assembly Lang
Sun Mon Tue Wed Thu Fri Sat
Structures and Union.
1/○~1/○ weekly schedule MON TUE WED THU FRI SAT SUN MEMO
Bitwise Operators.
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
C Language B. DHIVYA 17PCA140 II MCA.
Sun Mon Tue Wed Thu Fri Sat
Bit Manipulations CS212.
2008 Calendar.
Presentation transcript:

Enumerations

Review two dimensional array char *p[2] = {“MB”, “Mercedes Bentz”};

Review – function pointer

Bit Operations most CPU processes data in word unit 32 bits are processed in parallel if you need just one bit of information, the remaining space for 31 bits is wasted

C Bitwise Operators

Bitwise Complement int a = 70707; a ~a 00000000 00000001 00010100 00110011 ~a 11111111 11111110 11101011 11001100 -70708

2’s Complement int a = 70707; a ~a 2’s 00000000 00000001 00010100 00110011 ~a 11111111 11111110 11101011 11001100 -70708 2’s 11111111 11111110 11101011 11001101 -70707

Bitwise Logical Operators

Shift Operators If you left-shift a signed number so that the sign bit is affected, the result is undefined

 For unsigned numbers, the bit positions are zero-filled For signed numbers, the sign bit is used to fill the vacated bit positions

Masks void bit_print(int a) { int i; int n = sizeof(int) * CHAR_BIT; /* in limits.h */ int mask = 1 << (n - 1); /* mask = 100...0 */ for (i = 1; i <= n; ++i) { putchar(((a & mask) == 0) ? '0' : '1'); a <<= 1; if (i % CHAR_BIT == 0 && i < n) putchar(' '); } bit_print(33333); 00000000 00000000 10000010 00110101

Packing /* Pack 4 characters into an int. */ #include <limits.h> int pack(char a, char b, char c, char d) { int p = a; /* p will be packed with a, b, c, d */ p = (p << CHAR_BIT) | b; p = (p << CHAR_BIT) | c; p = (p << CHAR_BIT) | d; return p; }

Unpacking /* Unpack a byte from an int. */ #include <limits.h> char unpack(int p, int k) /* k = 0, 1, 2, or 3 */ { int n = k * CHAR_BIT; /* n = 0, 8, 16, or 24 */ unsigned mask = 255; /* low-order byte */ mask <<= n; return ((p & mask) >> n); }

Enumeration Types enum day {sun, mon., tue, wed, thu, fri, sat); enum day d1, d2, holidays; enumerators are of int type the first one is zero, .. enum suit {club = 3, diamonds, hearts = 0; spades} a, b;

Enumeration Types type casting applies

Preprocessor works before compilation macros #include includes “file name” or search <file name> in other directories macros #define SEC_PER_DAY (60 * 60 * 24) #define SQ(x) ((x) * (x)) #define max(x, y) (((x) > (y)) ? (x) : (y))

Conditional Compilation set the value of win using #define

Other directives defined operator debugging code # if (defined(HP) || defined(SUN)) && !defined(SUN3) ...... #endif debugging code # if DEBUG #if HEAP_SIZE < 24000 # error “not enough space for malloc”

Stringization #define message(a, b) printf (#a “and “ #b); message(Hail, SKKU) is replaced by printf(“Hail” “and” “SKKU”) #define X(i) X ## i X(2) is replaced by X2