Bit Field.

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

CSC141- Introduction to Computer Programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Programming In C++ Spring Semester 2013 Lecture 12 Programming In C++, Lecture 12 By Umer Rana.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
INSTRUCTION SET ARCHITECTURES
The Linux Kernel: Memory Management
The Assembly Language Level
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
18-May-15 Numbers. 2 Bits and bytes A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad One bit can distinguish.
© 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
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
Introduction to C Programming CE Lecture 2 Basics of C programming.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Embedded ‘C’.  It is a ‘mid-level’, with ‘high-level’ features (such as support for functions and modules), and ‘low-level’ features (such as good access.
Data Types in the Kernel Sarah Diesburg COP 5641.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
Representing numbers and Basic MATLAB 1. Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g.,
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Storing and Organizing Data. Why Do I Need to Understand How Data Is Represented? In order to install, program,maintain, and troubleshoot today’s PLCs,
Week 1 Algorithmization and Programming Languages.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 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 Unions and Bitfields Gabriel Hugh Elkaim Spring 2013.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
Week 10 - Wednesday.  What did we talk about last time?  Linked list implementations  Started doubly linked lists.
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 –
Variables Symbol representing a place to store information
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
Integer VariablestMyn1 Integer Variables It must be possible to store data items in a program, and this facility is provided by variables. A variable is.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
MIPS Arithmetic is 32 bits
Chapter # 2 Part 2 Programs And data
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
How Computers Store Variables
Programming Fundamentals
EPSII 59:006 Spring 2004.
Fundamental Data Types
Conversions of the type of the value of an expression
Unit 2 Programming.
C Structures, Unions, Bit Manipulations and Enumerations
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
C Structures, Unions, Bit Manipulations and Enumerations
Fundamental Data Types
Software Development Environment, File Storage & Compiling
Variables in C Topics Naming Variables Declaring Variables
Number Systems and Circuits for Addition
Variables and Constants
Storage Classes.
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

Bit Field

Bit Field C allow to store bits of data by dividing the memory space of integer (2bytes), using a structure. These space-saving structure members are called bit fields, and their width in bits can be explicitly declared. A structure declarator can also be a specified number of bits, called a "bit field." Its length is set off from the declarator for the field name by a colon. A bit field is interpreted as an integral type.

Bit Field Example struct mybitfields { unsigned short a : 4; unsigned short b : 5; unsigned short c : 7; } test; main( ) clrscr(); test.a = 2; test.b = 31; test.c = 0; printf("%d %d %d",test.a,test.b,test.c); getch(); } the bits would be arranged as follows: 00000001 11110010 Ccccccc bbbbb aaaa

Bit Field struct packed_struct { unsigned int f1:1; unsigned int f2:1; unsigned int type:4; unsigned int funny_int:9; } pack; Here the packed_struct contains 6 members: Four 1 bit flags f1..f3, a 4 bit type and a 9 bit funny_int. C automatically packs the above bit fields as compactly as possible, provided that the maximum length of the field is less than or equal to the integer word length of the computer. If this is not the case then some compilers may allow memory overlap for the fields whilst other would store the next field in the next word (see comments on bit fiels portability below).

Bit Field Access members as usual via: pack.type = 7; NOTE: Only n lower bits will be assigned to an n bit number. So type cannot take values larger than 15 (4 bits long). Bit fields are always converted to integer type for computation. You are allowed to mix ``normal'' types with bit fields. The unsigned definition is important - ensures that no bits are used as a flag.

Bit Field struct DISK_REGISTER { unsigned ready:1; unsigned error_occured:1; unsigned disk_spinning:1; unsigned write_protect:1; unsigned head_loaded:1; unsigned error_code:8; unsigned track:9; unsigned sector:5; unsigned command:5; };