Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Bitwise Operations and Miscellaneous Topics CS-2301 D-term 20091 Bitwise Operations and Miscellaneous Topics CS-2301 System Programming D-term 2009 (Slides."— Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

25 Bitwise Operations and Miscellaneous Topics CS-2301 D-term 200925 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 –…

26 Bitwise Operations and Miscellaneous Topics CS-2301 D-term 200926 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 –…

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

Similar presentations


Ads by Google