Bit Fields & Bitwise Operations

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

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
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
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.
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.
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
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.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
Bit Fields & Bitwise Operations CS-2303, C-Term Bit Fields & Bitwise Operations CS-2303 System Programming Concepts (Slides include materials from.
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
Free Ebooks Download Mba Ebooks By Edhole Mba ebooks Free ebooks download
 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.
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.
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
Bit Operations Horton pp
Programming Assignment #4 Binary Trees in C++
C Structures, Unions, Bit Manipulations and Enumerations
More about Numerical Computation
Chapter 14 Bitwise Operators Objectives
Makefiles and Notes on Programming Assignment PA2
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
“Under the Hood” of Polymorphism
Classes, Constructors, etc., in C++
Containers and the Standard Template Library (STL)
Miscellaneous C++ Topics
Structures, Unions, and Typedefs
Arrays and Pointers in C & C++
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Linked Lists in C and C++
Binary Trees (and Big “O” notation)
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Programming Assignment #1 12-Month Calendar—
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Symbolic Constants in C
Programming Assignment #6
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Scope Rules and Storage Types
Bit-wise and bit-shift operators
Programming Assignment #5
Differences between Java and C
Homework Homework Continue Reading K&R Chapter 2 Questions?
Your first C and C++ programs
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Digression on Loop Invariants
Bitwise operators.
A Deeper Look at Classes
Module 10 Operations on Bits
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Bit Manipulations CS212.
Bit Operations Horton pp
Presentation transcript:

Bit Fields & Bitwise Operations Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Bit Fields & Bitwise Operations

Bit Fields & Bitwise Operations See §2.9 and §6.9 in K&R 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 CS-2303, A-Term 2012 Bit Fields & Bitwise Operations 2

Bitwise Operations in Integers Corresponding bits of both operands are combined by the usual logic operations. & – 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 Apply to all kinds of integer types:– Signed and unsigned char, short, int, long, long long CS-2303, A-Term 2012 Bit Fields & Bitwise Operations 3

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

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

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

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

Printer Status Register (continued) Empty paper Paper jam Low ink Clean 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 */ CS-2303, A-Term 2012 Bit Fields & Bitwise Operations 8

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 CS-2303, A-Term 2012 Bit Fields & Bitwise Operations 9

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

“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 CS-2303, A-Term 2012 Bit Fields & Bitwise Operations

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

Printer Status Register (cont.) Empty paper Paper jam Low ink Clean count 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; }; CS-2303, A-Term 2012 Bit Fields & Bitwise Operations 13

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; CS-2303, A-Term 2012 Bit Fields & Bitwise Operations 14

Questions about Bit Fields? CS-2303, A-Term 2012 Bit Fields & Bitwise Operations 15