View
222
Download
2
Embed Size (px)
Citation preview
Bitwise Operations and Miscellaneous Topics
CS-2301 D-term 2009 1
Bitwise Operations andMiscellaneous Topics
CS-2301 System Programming D-term 2009
(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel)
Bitwise Operations and Miscellaneous Topics
CS-2301 D-term 2009 2
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 2009 3
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 2009 4
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 2009 5
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 2009 6
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 2009 7
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 2009 8
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 2009 9
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 2009 10
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 2009 11
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 2009 12
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 2009 14
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 2009 15
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 2009 16
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 2009 17
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 2009 18
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 2009 19
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 2009 20
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 2009 21
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 2009 22
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 2009 23
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 2009 24
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 2009 25
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%tabif (*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 2009 26
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 2009 27
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 2009 28
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 2009 29
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 2009 30
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 2009 31
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 2009 32
Stepwise Refinement (again)
• First attempts at top-down approach were wrong!
Bitwise Operations and Miscellaneous Topics
CS-2301 D-term 2009 33
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 2009 34
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 2009 35
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 2009 36
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 2009 37
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 i
n this part.
Until we can, solution does not yet
exist.
Bitwise Operations and Miscellaneous Topics
CS-2301 D-term 2009 38
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 2009 39
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 2009 40
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 2009 41
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 2009 42
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 2009 43
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 2009 44
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
• …