Bit Fields & Bitwise Operations CS-2303, C-Term 20101 Bit Fields & Bitwise Operations CS-2303 System Programming Concepts (Slides include materials from.

Slides:



Advertisements
Similar presentations
OPTIMIZING C CODE FOR THE ARM PROCESSOR Optimizing code takes time and reduces source code readability Usually done for functions that are critical for.
Advertisements

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 3 GETTING THE MOST OUT OF C.
COMP3221 lec9-logical-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 9: C/Assembler Logical and Shift - I
Event-drive SimulationCS-2303, C-Term Project #3 – Event-driven Simulation CS-2303 System Programming Concepts (Slides include materials from The.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Loose endsCS-2301, B-Term “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
Programming Assignment #4 Binary Trees
"Loose ends"CS-2301 D-term “Loose Ends” CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming Language, 2 nd edition,
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Logical & shift ops (1) Fall 2007 Lecture 05: Logical Operations.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
Bitwise Operations and Miscellaneous Topics CS-2301 D-term Bitwise Operations and Miscellaneous Topics CS-2301 System Programming D-term 2009 (Slides.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Symbolic Constants in CCS-2303, C-Term Digression – Symbolic Constants in C CS-2303, System Programming Concepts (Slides include materials from The.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
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.
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
CS-2303 System Programming Concepts
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
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.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Copyright © Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus.
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Computer Organization and Assembly Language Bitwise Operators.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #6 Structures,
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
CSC321 Homework Due Due Tuesday after spring break Turn in –Design information State diagram State table K-map simplifications and expressions for flip-flop.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Free Ebooks Download Mba Ebooks By Edhole Mba ebooks Free ebooks download
Comparison Instructions Test instruction –Performs an implied AND operation between each of the bits in 2 operands. Neither operand is modified. (Flags.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Bit Manipulation in 'C' 'C' bit operators
Microprocessor & Assembly Language
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Data Representation. Representation of data in a computer Two conditions: 1. Presence of a voltage – “1” 2. Absence of a voltage – “0”
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
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;
Operator Kinds of Operator Precedence of Operator Type Casting.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
Chapter 4 Operations on Bits.
Instructor: David Ferry
Session #5 File I/O Bit Masks, Fields & Bit Manipulations
Bit Operations Horton pp
Chapter 3 Bit Operations
C Structures, Unions, Bit Manipulations and Enumerations
More about Numerical Computation
Chapter 14 Bitwise Operators Objectives
The University of Adelaide, School of Computer Science
Introduction to Programming
CS-401 Computer Architecture & Assembly Language Programming
Bit Fields & Bitwise Operations
Differences between Java and C
Homework Homework Continue Reading K&R Chapter 2 Questions?
CISC/CMPE320 - Prof. McLeod
Bitwise operators.
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Bit Manipulations CS212.
Bit Operations Horton pp
Presentation transcript:

Bit Fields & Bitwise Operations CS-2303, C-Term Bit Fields & Bitwise Operations CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Bit Fields & Bitwise Operations CS-2303, C-Term Bitwise Operations See §2.9 and §6.9 in K&R See §10.9 in D&D Many situations, need to operate on the bits of a data word – Register inputs or outputs Controlling attached devices Obtaining status Especially ECE-2801 and ECE-3803

Bit Fields & Bitwise Operations CS-2303, C-Term Review – Bitwise Operations in Integers & – AND Result is 1 if both operand bits are 1 | – OR Result is 1 if either operand bit is 1 ^ – Exclusive OR Result is 1 if operand bits are different ~ – Complement Each bit is reversed << – Shift left Multiply by 2 >> – Shift right Divide by 2 Corresponding bits of both operands are combined by the usual logic operations. Apply to all kinds of integer types:– Signed and unsigned char, short, int, long, long long

Bit Fields & Bitwise Operations CS-2303, C-Term Examples unsigned int c, a, b; c = a & b; c = a | b; c = a ^ b; c = ~a; c = a << 2; c = a >> 3; a b

Bit Fields & Bitwise Operations CS-2303, C-Term Right Shift is Tricky unsigned int c, a; c = a >> 3; signed int c, a, b; c = b >> 3; c = a >> 3; a a b

Bit Fields & Bitwise Operations CS-2303, C-Term Two Approaches Traditional C Use #define and a lot of bitwise operations Modern Use bit fields Much more frequent in real world!

Bit Fields & Bitwise Operations CS-2303, C-Term Example – Printer Status Register Traditional C definition of bit fields #define EMPTY 01 #define JAM 02 #define LOW_INK 16 #define CLEAN 64 Empty paper Paper jam Low ink Clean

Bit Fields & Bitwise Operations CS-2303, C-Term Example – Printer Status Register (cont.) Traditional bit fields (continued) char status; if (status == (EMPTY | JAM))...; if (status == EMPTY || status == JAM)...; while (! status & LOW_INK)...; int flags |= CLEAN/* turns on CLEAN bit */ int flags &= ~JAM/* turns off JAM bit */ Empty paper Paper jam Low ink Clean

Bit Fields & Bitwise Operations CS-2303, C-Term Traditional Bit Definitions Used very widely in C Including a lot of existing code No checking You are on your own to be sure the right bits are set Machine dependent Need to know bit order in bytes, byte order in words Integer fields within a register Need to AND and shift to extract Need to shift and OR to insert

Bit Fields & Bitwise Operations CS-2303, C-Term Example – Printer Status Register (cont.) An integer field (traditional style) #define COUNT (8|16|32|64|128) int c = (status & COUNT) >> 3; status |= (c << 3) & COUNT; Empty paper Paper jam Low ink Clean count

Bit Fields & Bitwise Operations CS-2303, C-Term “Modern” Bit-Field Definitions See Kernighan & Ritchie, §6.9 Like a struct, except Each member is a bit-field within a word Accessed like members of a struct Fields may be named or unnamed Machine-dependent Order of bits in word Size of word

Bit Fields & Bitwise Operations CS-2303, C-Term Modern Bit-field Definitions struct statusReg { unsigned int emptyPaperTray :1; unsigned int paperJam :1; :2; unsigned int lowInk :1; :1; unsigned int needsCleaning :1; :1; }; Empty paper Paper jam Low ink Clean

Bit Fields & Bitwise Operations CS-2303, C-Term Example – Printer Status Register (cont.) struct statusReg { unsigned int emptyPaperTray :1; unsigned int paperJam :1; :1; unsigned int count :5; :1; unsigned int lowInk :1; :1; unsigned int needsCleaning :1; :1; }; Empty paper Paper jam Low ink Clean count

Bit Fields & Bitwise Operations CS-2303, C-Term Modern Bit-fields (continued) struct statusReg s; if (s.empty && s.jam)...; while(! s.lowInk)...; s.needsCleaning = true; s.Jam = false; int c = s.count; s.count -= 1;

Bit Fields & Bitwise Operations CS-2303, C-Term Questions about Bit Fields?