Lecture 10 union, enumeration, bit

Slides:



Advertisements
Similar presentations
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Advertisements

C Language.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Structure of a C program
Variables and constants Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Dennis Ritchie 1972 AT & T Bell laboratories (American Telephone and Telegraph) USA 1www.gowreeswar.com.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
© Oxford University Press All rights reserved. CHAPTER 8 STRUCTURES.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
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.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
Chapter 8 Arrays, Strings and Pointers
11 Chapter Structured Data
Data types Data types Basic types
ECE Application Programming
Week 3 - Friday CS222.
Structure, Unions, typedef and enumeration
Documentation Need to have documentation in all programs
Fundamental of Programming (C)
Variable Declaration, Data types, Expressions
C Fundamentals & Pointers
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Short Overview Lembit Jürimägi.
C Basics.
Module 2 Arrays and strings – example programs.
សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management
Introduction to C Programming
DATA HANDLING.
Variable Declarations, Data types, Expressions
Buy book Online -
Lecture 9 Structure 1. Concepts of structure Pointers of structures
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,
Lecture 8.
Structures and Union.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Chapter 11: Structured Data.
Introduction to C Programming
UMBC CMSC 104 – Section 01, Fall 2016
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Chapter 1: Introduction to Data Structures(8M)
Variables in C Declaring , Naming, and Using Variables.
Structures and Union.
C Structures, Unions, Bit Manipulations and Enumerations
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
POINTER CONCEPT 4/15/2019.
Bitwise operators.
Engineering H192 Winter 2005 Structs and Enumeration Prof. Almas Ansari Dept of Computer Science Engineering Anjuman College of Engineering & Technology..
Standard Version of Starting Out with C++, 4th Edition
Structures and Union.
Variables in C Topics Naming Variables Declaring Variables
C Language B. DHIVYA 17PCA140 II MCA.
POINTER CONCEPT 8/3/2019.
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Getting Started With Coding
Presentation transcript:

Lecture 10 union, enumeration, bit 1. Concepts of union Concepts of enumeration Bitwise operations Bit fields

1. Concepts of Unions Union is a user-defined data type that can store various types of data at the same memory location Similar to structures, a union is a collection of variables of different types Different from structure, only one type of the data can be stored in one field at any one time The sizeof union type is the maximum size of its variable types

1. Concepts of Unions Unions are used to save memory. They are useful for applications that involve multiple members, where values need not be assigned to all the members at any one time. Syntax union union-name { data_type var-name; ................................ };

Example: or union record { int i; char ch; float f; }; union record a; union record { int i; char ch; float f; } a; sizeof(a) = ? sizeof(record) = ? or union record { int i; char ch; float f; }; union record a; or typedef union { int i; char ch; float f; } record; record a;

record a; declare a memory space named a , a can be used to store an integer, char, or float number, can only hold one of them a time. The sizeof a is 4

Example record b; // can not be initialized b.i = 10; // b holds an integer, wrong syntax: b = 10; printf(“%d”, b.i); b.f= 10.5; // b holds an integer, wrong syntax: b = 10.5; float y; y= b.f ; b.ch= ‘A’; // b holds an integer, not b = ‘A’; printf(“%d”, b.ch);

Initializing Unions #include <stdio.h> typedef union { int x; int y; } POINT2; int main() { POINT2 P2; P2. x = 4; printf(“\n The x coordinate of P2 is %d”, P2.x); P2.y = 5; printf(“\n The y coordinate of P2 is %d”, P2.x); return 0; }

Arrays of Unions #include <stdio.h> union POINT { int x, y; }; int main() { int i; union POINT points[3]; points[0].x = 2; points[0].y = 3; points[1].x = 4; points[1].y = 5; points[2].x = 6; points[2].y = 7; for(i=0;i<3;i++) printf(“\n Coordinates of Point[%d] are %d and %d”, i, points[i].x, points[i].y); return 0; }

Unions inside Structures struct student { union { char name[20]; int roll_no; }; int marks; } st; /* in main() function*/ printf(“\n You can enter the name or roll number of the student”); printf(“\n Do you want to enter the name? (Y or N): ”); gets(choice); if(choice==‘y’ || choice==‘Y’) { printf(“\n Enter the name: ”); gets(st.name); } else { printf(“\n Enter the roll number: ”); scanf(“%d”, &stud.roll_no);

class code example person[0].id = 101; strcpy(person[0].name, “John”); person[0].job = ‘s’; person[0].category.class = 264; person[1].id = 102; strcpy(person[1].name, “Peter”); person[1].job = ‘t’; strcpy(person[1].category.position, “professor”); typedef union { int class; position[10]; } pType; typedef struct{ int id; char name[20]; char job; pType category; } recType; recType person[2];

printf(“ID Name Job Class/Position”); for (i = 0; i < 2; i++){ if (person[i].job ==‘s’) printf(“%-6d %-10s %-3c %-6s\n”, person[i].id, person[i].name, person[i].gender, person[i].job, person[0].category.class ); else printf(“%-6d %-10s %-3c %-6s\n”, person[i].id, person[i].name, person[i].gender, person[i].job, person[0].category.position ); } ID Name Job Class/Position John s 264 Peter t professor

2. Concepts of Enumeration enumeration is a user-defined data type that consists of integral constants. Used when a data only have a few possible values, enumeration data can be used. The sizeof emumeration datatype is equals to sizeof(int) Syntax, enum enum_name { const1, const2, ..., constN };

Examples enum BOOLEAN { false, true } flag; // internally false is assigned value 0, true value 1 flag = true; printf(“%d”, flag); // output 1 typedef enum BOOLEAN boolean; boolean flag1 = false; printf(“%d”, flag1); // output 0 or typedef enum { false, true } boolean; boolean flag = true;

Another example typedef enum { /* Defines an enumeration type */ saturday =1 , /* Names day and declares a */ sunday , /* variable named workday with monday, /* that type */ Tuesday =5, wednesday, /* wednesday is associated with 6 */ thursday, friday , } dayType; dayType today = wednesday; dayType nextday = today+1; printf(“%d”, nextday); // will print value 7 enum { yes, no } response;

3. Bitwise operations in C A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits Bitwise operation is a fast, primitive operation directly supported by the processor It is used to manipulate values for comparisons and calculations.

Available bitwise operations in C unsigned int a, b, c; & AND e.g. c = a & b; &= AND Assignment, e.g. a &= b;  a = a & b; | OR e.g. c = a | b; |= OR Assignment e.g. a |= b;  a = a | b; ^ XOR e.g. c = a ^ b; ^= XOR Assignment e.g. a ^= b;  a = a ^ b; ~ one's compliment e.g c = ~ a; << Shift Left e.g c = a << 2; >> Shift Right e.g. c = a >> 2; <<= Shift Left Assignment e.g. a <<= 2;  a = a << 2; >>= Shift Right Assignment e.g a >>= a;  a = a >> 2;

Examples int main() { unsigned int a = 60; /* 60 = 0000 0000 0011 1100 */ unsigned int b = 13; /* 13 = 0000 0000 0000 1101 */ unsigned int c = 0; c = a & b; /* 12 = 0000 0000 0000 1100 */ } unsigned int a = 60; /* 60 = 0000 0000 0011 1100 */ unsigned int b = 13; /* 13 = 0000 0000 0000 1101 */ c = a | b; /* 61 = 0000 0000 0011 1101 */

XOR and one’s complement main() { unsigned int a = 60; /* 60 = 0000 0000 0011 1100 */ unsigned int b = 13; /* 13 = 0000 0000 0000 1101 */ unsigned int c = 0; c = a ^ b; /* 49 = 0000 0000 0011 0001 */ } unsigned int Value=4; /* 4 = 0000 0000 0000 0100 */ Value = ~ Value; /* 251 = 1111 1111 1111 1011 */

Example of shift main() { unsigned int value=4; /* 4 = 0000 0000 0000 0100 */ unsigned int shift=2; value = value << shift; /* 16 = 0000 0000 0001 0000 */ value <<= shift; /* 64 = 0000 0000 0100 0000 */ printf("%d\n", value); /* Prints 64 */ }

Example of shift int main() { unsigned long int bytes=256; /* 00000000 00000000 00000000 10000000 */ do printf("%3ld \n", bytes); bytes >>= 1; /* 00000000 00000000 00000000 01000000 */ } while (bytes); } 256 128 64 32 16 8 4 2 1

4. Bit fields A bit field is used in computer programming to compactly store multiple logical values as a short series of bits where each of the single bits can be addressed separately A bit field is most commonly used to represent integral types of known, fixed bit-width A well-known usage of bit-fields is to represent single bit flags with each flag stored in a separate bit

The application of bit fields in IP header

Example of using bit fields typedef struct packet-data { unsigned a:2; // has has 2 bits unsigned b:6; // has has 6 bits unsigned c:4; // has has 4 bits unsigned d:4; // has has 4 bits int i:16; // has 16 bits } packetType; packetType data; data.a = 3; data.b = 7; data.c = 9; data.d = 15; data.i = 256; printf(“%d, %d, %d, %d”, data.a, data.b, data.c, data.d); printf(“%u, %o, %x, %d”, data.a, data.b, data.c, data.d); a b c d i 1 1 0 0 0 1 1 1 1 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0