CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Unions and Bitfields Gabriel Hugh Elkaim Spring 2013.

Slides:



Advertisements
Similar presentations
Structure.
Advertisements

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.
Structures Spring 2013Programming and Data Structure1.
Bit Field.
Union, bitfield, typedef, enum union nama_u{ }; union nama_u{ struct nama_s byte; }; enum{ }; Tipedef var BYTE.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Structure of a C program
String Escape Sequences
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.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Functions Gabriel Hugh Elkaim Spring 2012.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Pre-Processor Commands Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Modules and Scope Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Constants and Literals Gabriel Hugh Elkaim Spring 2012.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
Lecture 2 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout Data types.
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Structures Gabriel Hugh Elkaim Spring 2013.
M.T.Stanhope Oct Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Introduction to “C” Programming Gabriel Hugh Elkaim Spring 2012.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Pointers Gabriel Hugh Elkaim Spring 2012.
Array, Structure and Union
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Homework Finishing up K&R Chapter 6 today Also, K&R 5.7 – 5.9 (skipped earlier)
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Computer Systems and “C” Programming Gabriel Hugh Elkaim Spring 2013.
The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.
Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
Structures and Unions in C Alan L. Cox
Week 10 - Wednesday.  What did we talk about last time?  Linked list implementations  Started doubly linked lists.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Enumerations Gabriel Hugh Elkaim Spring 2013.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
ECE Application Programming
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Exercises in MPLAB-X Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Function Pointers Gabriel Hugh Elkaim Spring 2013.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
COMP 1402 Winter 2008/Summer 2008 Tutorial #10 Enumerations, Unions, Linked Lists.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
Lecture 10 union, enumeration, bit
EGR 2261 Unit 11 Pointers and Dynamic Variables
CSE 220 – C Programming Bitwise Operators.
The Machine Model Memory
Memory, Data, & Addressing II CSE 351 Autumn 2017
EPSII 59:006 Spring 2004.
DATA HANDLING.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures and Union.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
CS-401 Computer Architecture & Assembly Language Programming
Bits and Bytes Topics Representing information as bits
Memory, Data, & Addressing II CSE 351 Summer 2018
Structures and Union.
Recitation 5 – 2/19/01 Outline
C Structures, Unions, Bit Manipulations and Enumerations
Structures and Union.
Presentation transcript:

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Unions and Bitfields Gabriel Hugh Elkaim Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 DefinitionUnions Unions: –May contain any number of members –Members may be of any data type –Are as large as their largest member –Use exactly the same syntax as structures except struct is replaced with union Unions are similar to structures but a union’s members all share the same memory location. In essence a union is a variable that is capable of holding different types of data at different times.

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Syntax Example Unions How to Create a Union // Union of char, int and float union mixedBag { char a; char a; int b; int b; float c; float c;} union unionName { type 1 memberName 1 ; type 1 memberName 1 ; type n memberName n ; type n memberName n ;}

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Syntax Example Unions How to Create a Union Type with typedef // Union of char, int and float typedef union { char a; char a; int b; int b; float c; float c; } mixedBag; typedef union unionTag optional { type 1 memberName 1 ; type 1 memberName 1 ; type n memberName n ; type n memberName n ; } typeName;

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example typedef union { char a; char a; int b; int b; float c; float c; } mixedBag; mixedBag x; Unions How Unions Are Stored In Memory Union variables may be declared exactly like structure variables Memory is only allocated to accommodate the union’s largest member x 16-bit Data Memory (RAM) Space allocated for x is size of float 0x800 0x802 0x7FF 0x804

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example typedef union { char a; char a; int b; int b; float c; float c; } mixedBag; mixedBag x; Unions How Unions Are Stored In Memory Union variables may be declared exactly like structure variables Memory is only allocated to accommodate the union’s largest member x 16-bit Data Memory (RAM) x.a only occupies the lowest byte of the union 0x800 0x802 0x7FF 0x804

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example typedef union { char a; char a; int b; int b; float c; float c; } mixedBag; mixedBag x; Unions How Unions Are Stored In Memory Union variables may be declared exactly like structure variables Memory is only allocated to accommodate the union’s largest member x 16-bit Data Memory (RAM) x.b only occupies the lowest two bytes of the union 0x800 0x802 0x7FF 0x804

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example typedef union { char a; char a; int b; int b; float c; float c; } mixedBag; mixedBag x; Unions How Unions Are Stored In Memory Union variables may be declared exactly like structure variables Memory is only allocated to accommodate the union’s largest member x 16-bit Data Memory (RAM) x.c occupies all four bytes of the union 0x800 0x802 0x7FF 0x804

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Lab Exercise 16 Unions

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 16 Unions Open the project’s workspace: 1 1 Open MPLAB ® and select Open Workspace… from the File menu. Open the file listed above. If you already have a project open in MPLAB, close it by selecting Close Workspace from the File menu before opening a new one. /Examples/Lab16.zip -> Load “Lab16.mcw” On the class website

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 16 Unions /*############################################################################ # STEP 1: Set the int member of unionVar equal to ############################################################################*/ //Set intVar = unionVar.intVar = 16877; /*############################################################################ # STEP 2: Set the float member of unionVar equal to 6.02e23. ############################################################################*/ //Set floatVar = 6.02e23 unionVar.floatVar = 6.02e23; Solution: Steps 1 and 2

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 16 Conclusions Unions make it possible to store multiple variables at the same location They make it possible to access those variables in different ways They make it possible to store different variable types in the same memory location(s)

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Definition Bit Fields Bit Fields: –Are ordinary members of a structure –Have a specified bit width –Are often used in conjunction with unions to provide bit access to a variable without masking operations Bit Fields are unsigned int members of structures that occupy a specified number of adjacent bits from one to sizeof(int). They may be used as an ordinary int variable in arithmetic and logical operations.

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Syntax Example Bit Fields How to Create a Bit Field typedef struct { unsigned int bit0: 1; unsigned int bit0: 1; unsigned int bit1to3: 3; unsigned int bit1to3: 3; unsigned int bit4: 1; unsigned int bit4: 1; unsigned int bit5: 1; unsigned int bit5: 1; unsigned int bit6to7: 2; unsigned int bit6to7: 2; } byteBits; struct structName { unsigned int memberName 1 : bitWidth; unsigned int memberName 1 : bitWidth; unsigned int memberName n : bitWidth; unsigned int memberName n : bitWidth;} bitfield struct may be declared normally or as a typedef

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Bit Fields How to Use a Bit Field struct byteBits { unsigned a: 1; unsigned a: 1; unsigned b: 1; unsigned b: 1; unsigned c: 2; unsigned c: 2; unsigned d: 1; unsigned d: 1; unsigned e: 3; unsigned e: 3; } x; int main(void) { x.a = 1; //x.a may contain values from 0 to 1 x.a = 1; //x.a may contain values from 0 to 1 x.b = 0; //x.b may contain values from 0 to 1 x.b = 0; //x.b may contain values from 0 to 1 x.c = 0b10; //x.c may contain values from 0 to 3 x.c = 0b10; //x.c may contain values from 0 to 3 x.d = 0x0; //x.d may contain values from 0 to 1 x.d = 0x0; //x.d may contain values from 0 to 1 x.e = 7; //x.e may contain values from 0 to 7 x.e = 7; //x.e may contain values from 0 to 7} x Byte in Data Memory (RAM) abcde

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Lab Exercise 17 Bit Fields

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 17 Bit Fields Open the project’s workspace: 1 1 Open MPLAB ® and select Open Workspace… from the File menu. Open the file listed above. If you already have a project open in MPLAB, close it by selecting Close Workspace from the File menu before opening a new one. /Examples/Lab17.zip -> Load “Lab17.mcw” On the class website

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 17 Bit Fields Compile and run the code: 2 2 Click on the Build All button. Compile (Build All) Run If no errors are reported, click on the Run button. Halt Click on the Halt button.

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 17 Bit Fields /* VARIABLE DECLARATIONS */ union { char fullByte; struct { int bit0: 1; int bit1: 1; int bit2: 1; int bit3: 1; int bit4: 1; int bit5: 1; int bit6: 1; int bit7: 1; } bitField; } bitByte; Bit Field Definition

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 17 Bit Fields Demo Results 1 bitByte.fullByte = 0x55;

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 17 Bit Fields Demo Results 2 bitByte.bitField.bit0 = 0;

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 17 Bit Fields Demo Results 3 bitByte.bitField.bit2 = 0;

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 17 Bit Fields Demo Results 4 bitByte.bitField.bit7 = 1;

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 17 Conclusions Bit fields provide an efficient mechanism to store Boolean values, flags and semaphores in data memory Care must be used if code size or speed is a concern –Compiler will usually make use of bit set / bit clear instructions –In some circumstances this isn't possible (comparing bit values)

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Questions?

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013