Bitwise Operations and Miscellaneous Topics CS-2301 D-term 20091 Bitwise Operations and Miscellaneous Topics CS-2301 System Programming D-term 2009 (Slides.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

INSTRUCTION SET ARCHITECTURES
CS252: Systems Programming Ninghui Li Program Interview Questions.
Homework #4CS-2301 B-term Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The.
Structures, Unions, and Typedefs CIS 1057 Fall Structures, Unions, and Typedefs CIS 1057 Computer Programming in C Fall 2013 (Many slides based on/borrowed.
Strings and Dynamic Memory Allocation CS-2301, B-Term Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Lists and TreesCS-2301 D-term Data Structures — Lists and Trees CS-2301 System Programming D-term 2009 (Slides include materials from The C Programming.
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Introduction to FunctionsCS-2301 D-term Introduction to Functions CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming.
Loose endsCS-2301, B-Term “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
"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.
Introduction to FunctionsCS-2301 B-term Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.
Structures and UnionsCS-2301 B-term Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
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.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
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 –
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Chapter 18 – Miscellaneous Topics. Multiple File Programs u Makes possible to accommodate many programmers working on same project u More efficient to.
Chapter 3 Flow Control By C. Shing ITEC Dept Radford University.
Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control.
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
Bit Fields & Bitwise Operations CS-2303, C-Term Bit Fields & Bitwise Operations CS-2303 System Programming Concepts (Slides include materials from.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Introduction to FunctionsCIS 1057 Fall Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Free Ebooks Download Mba Ebooks By Edhole Mba ebooks Free ebooks download
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
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;
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
The Machine Model Memory
C Programming Tutorial – Part I
Session #5 File I/O Bit Masks, Fields & Bit Manipulations
C Basics.
KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY
CS 11 C track: lecture 8 Last week: hash tables, C preprocessor
Some Basics for Problem Analysis and Solutions
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to C++ Programming
More about Numerical Computation
Introduction to Primitive Data types
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Input and Output.
Bit Fields & Bitwise Operations
Programming Assignment #1 12-Month Calendar—
Differences between Java and C
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Introduction to Primitive Data types
Presentation transcript:

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Bitwise Operations and Miscellaneous Topics CS-2301 System Programming D-term 2009 (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)

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Bitwise Operations See §2.9 and §6.9 in Kernighan & Ritchie Many situation, need to operate on the bits of a data word – Register inputs or outputs Controlling attached devices Obtaining status

Bitwise Operations and Miscellaneous Topics CS-2301 D-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 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.

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Examples unsigned int c, a, b; c = a & b; c = a | b; c = a ^ b; b = ~a; c = a << 2; b = a >> 3;

Bitwise Operations and Miscellaneous Topics CS-2301 D-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

Bitwise Operations and Miscellaneous Topics CS-2301 D-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

Bitwise Operations and Miscellaneous Topics CS-2301 D-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

Bitwise Operations and Miscellaneous Topics CS-2301 D-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

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Alternative 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

Bitwise Operations and Miscellaneous Topics CS-2301 D-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

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Alternative 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;

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Warning Almost everything about bit fields is implementation dependent. Especially the order of fields in the struct ! Consult your hardware and compiler implementation!

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Questions about Bit Fields?

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Revisit Programming Assignment #5 A difficult learning exercise Messy algorithm Lots of states No apparent clean solution Not the kind of cut-and-dried problem assignment typical of this level of course Lessons are deep and subtle

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Ways to Approach a Programming Problem Top-down I.e., stepwise refinement Bottom-up I.e., work out the principle algorithm, and then build the system infrastructure around it Data-oriented I.e., define the shape and flow of the data, derive the algorithm from it

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Ways to Approach a Programming Problem Top-down I.e., stepwise refinement Bottom-up I.e., work out the principle algorithm, and then build the system infrastructure around it Data-oriented I.e., define the shape and flow of the data, derive the algorithm from it Fashionable when Professor was young.

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Ways to Approach a Programming Problem Top-down I.e., stepwise refinement Bottom-up I.e., work out the principle algorithm, and then build the system infrastructure around it Data-oriented I.e., define the shape and flow of the data, derive the algorithm from it Fashionable in CS departments today.

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Ways to Approach a Programming Problem Top-down I.e., stepwise refinement Bottom-up I.e., work out the principle algorithm, and then build the system infrastructure around it Data-oriented I.e., define the shape and flow of the data, derive the algorithm from it Default approach by programmers of normal skill levels.

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Top-down Approach Definition:– Step-wise refinement –Partition global problem statement into a few “macro” steps –For each step, refine it into a few sub-steps –Continue (recursively) until you have the entire problem solved. Advocated by Edsger Dijkstra

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Application to Programming Assignment #5 int main(int argc, char **argv) { for(i = 1; i < argc; i++) { if (*argv[i] == '-') { // Process Command line switches } else { // Open File // ReadAndPrint file with width & tab // close file } } //for

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Application to Programming Assignment #5 (continued) int ReadAndPrint(FILE *in, int width, int tab) { while(/*not end of file*/) { // read one paragraph (ends in \n or EOF) // Justify and print one paragraph }

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Application to Programming Assignment #5 (continued) int ReadAndPrint(FILE *in, int width, int tab) { bool eof = false; while(!eof) { while((c = fgetc(in))!= EOF && c != '\n') { // append c to string // increase size of string if necessary // see code fragment from HW4 }; if (c == EOF) eof = true; // Justify and print one paragraph }

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Application to Programming Assignment #5 (continued) int JustifyPrint(char *s, int width, int tab) { bool endOfPara = false; while(!endOfPara) { // scan and copy to end of one line // expand tabs while copying }; if (*s == '\0') endOfPara = true; // print the line }

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Application to Programming Assignment #5 (continued) int JustifyPrint(char *s, int width, int tab) { bool endOfPara = false; while(!endOfPara) { // scan and copy to end of one line // expand tabs while copying }; if (*s == '\0') endOfPara = true; // print the line } Here is where stepwise refinement starts to break down: – Number of different states and variations becomes large.

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Programming Assignment #5 (continued) Issues and requirements for one line –Copy each character of string s to line buffer I.e., a character array large enough to hold a line –When copying ' \t ', fill in spaces to i%tab if (*s == '\t') do line[i++] = ' ' while (i%tab != 0); –Need to copy as many characters as fit in a line –…

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Programming Assignment #5 (continued) Issues and requirements (continued) –However, if line ends in a middle of a word Remove characters back to end of previous word Remember them so they can be copied to next line –Be sure to leading include spaces at beginning of paragraph But no leading spaces within a paragraph unless '\t' –Special case:– a “word” with no spaces is too long to fit on one line Usually occurs with URLs –Short lines at end of paragraph treated differently –…

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Stepwise Refinement for Programming Assignment #5 Works pretty well … … until we get to nitty-gritty of the core algorithm. And then, it is not clear whether data structure or algorithm work out.

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Stepwise Refinement for Programming Assignment #5 Works pretty well … … until we get to nitty-gritty of the core algorithm. And then, it is not clear whether data structure or algorithm work out. In fact, they didn’t work out on first 2-3 attempts

Bitwise Operations and Miscellaneous Topics CS-2301 D-term What about Bottom-up Design Start with an algorithm to scan one line directly from file input Handle the special circumstances When reading from input, how to handle characters that don’t fit at end of line And pass them to next line

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Bottom-up Design (continued) How do we deal with EOF and ' \n ', ? Need to communicate back up the function call stack Functions cannot return multiple values Need to pass information back by reference Very complex semantics, pre- and post-conditions

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Data-Oriented Design Scanning an input stream Need to “un-scan” when word does not fit at end of line Same problems as with Bottom-up Design

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Stepwise Refinement (again) First attempts at top-down approach were wrong!

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Application to Programming Assignment #5 (continued) int ReadAndPrint(FILE *in, int width, int tab) { while(/*not end of file*/) { // read one paragraph (ends in \n or EOF) // Justify and print one paragraph } At first, was not asking the right question here.

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Application to Programming Assignment #5 (continued) int ReadAndPrint(FILE *in, int width, int tab) { bool eof = false; while(!eof) { while((c = fgetc(in))!= EOF && c != '\n') { // append c to string // increase size of string if necessary // see code fragment from HW4 }; if (c == EOF) eof = true; // Justify and print one paragraph } Separated EOF from newline. Added another loop to address individual paragraphs. This loop needs to leave EOF in variable c

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Stepwise Refinement (again) First attempts at top-down approach were wrong! Needed to separate EOF from ' \n ' and add paragraph loop Not at all obvious on first attempt to develop the refinement Needed to bump into a brick wall in order to have enough information to do it right

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Stepwise Refinement (again) First attempts at top-down approach were wrong! Needed to separate EOF from ' \n ' and add paragraph loop Not at all obvious on first attempt to develop the refinement Needed to bump into a brick wall in order to have enough information to do it right Several times!

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Application to Programming Assignment #5 (again) int JustifyPrint(char *s, int width, int tab) { bool endOfPara = false; while(!endOfPara) { // scan and copy to end of one line // expand tabs while copying }; if (*s == '\0') endOfPara = true; // print that line } Let’s try to fill in this part. Until we can, solution does not yet exist.

Bitwise Operations and Miscellaneous Topics CS-2301 D-term GetOneLine() Inputs Pointer to string s, line buffer line Starting character position n Tab width and line length maxLen Output stream FILE *out Result Starting position of next line

Bitwise Operations and Miscellaneous Topics CS-2301 D-term GetOneLine() (continued) int GetOneLine(char *s, char *line, int n,...) { int lp, sp = n; for(lp = 0; s[sp]!='\0' && lp<maxLen; sp++) { if (s[sp] = '\t') {do line[lp++] = ' ' while (lp%tab != 0);} else line[lp++] = s[sp]; }; if (s[sp]=='\0') { line[lp] = '\0'; return sp;} else // scan backwards to end of last word // set line[lp] = '\0'; // scan forward to next non-blank, return sp; }

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Expand tabs! GetOneLine() (continued) int GetOneLine(char *s, char *line, int n,...) { int lp, sp = n; for(lp = 0; s[sp]!='\0' && lp<maxLen; sp++) { if (s[sp] = '\t') {do line[lp++] = ' ' while (lp%tab != 0);} else line[lp++] = s[sp]; }; if (s[sp]=='\0') { line[lp] = '\0'; return sp;} else // scan backwards to end of last word // set line[lp] = '\0'; // scan forward to next non-blank, return sp; }

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Copy character! GetOneLine() (continued) int GetOneLine(char *s, char *line, int n,...) { int lp, sp = n; for(lp = 0; s[sp]!='\0' && lp<maxLen; sp++) { if (s[sp] = '\t') {do line[lp++] = ' ' while (lp%tab != 0);} else line[lp++] = s[sp]; }; if (s[sp]=='\0') { line[lp] = '\0'; return sp;} else // scan backwards to end of last word // set line[lp] = '\0'; // scan forward to next non-blank, return sp; }

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Test for end of paragraph! GetOneLine() (continued) int GetOneLine(char *s, char *line, int n,...) { int lp, sp = n; for(lp = 0; s[sp]!='\0' && lp<maxLen; sp++) { if (s[sp] = '\t') {do line[lp++] = ' ' while (lp%tab != 0);} else line[lp++] = s[sp]; }; if (s[sp]=='\0') { line[lp] = '\0'; return sp;} else // scan backwards to end of last word // set line[lp] = '\0'; // scan forward to next non-blank, return sp; }

Bitwise Operations and Miscellaneous Topics CS-2301 D-term GetOneLine() (continued) int GetOneLine(char *s, char *line, int n,...) { int lp, sp = n; for(lp = 0; s[sp]!='\0' && lp<maxLen; sp++) { if (s[sp] = '\t') {do line[lp++] = ' ' while (lp%tab != 0);} else line[lp++] = s[sp]; }; if (s[sp]=='\0') { line[lp] = '\0'; return sp;} else // scan backwards to end of last word // set line[lp] = '\0'; // scan forward to next non-blank, return sp; }

Bitwise Operations and Miscellaneous Topics CS-2301 D-term GetOneLine() (continued) There is still more to do Scan backward through line and string –To find end of last word (= end of line) –To find start of next word (= start of next line) Be sure not to get confused by expanded tabs …

Bitwise Operations and Miscellaneous Topics CS-2301 D-term Discussion or Questions?