Bitwise Operations and Miscellaneous Topics - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

Bitwise Operations and Miscellaneous Topics

Description:

Fashionable when Professor was young. Bitwise Operations and Miscellaneous Topics ... Fashionable in CS departments today. Bitwise Operations and Miscellaneous Topics ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 48
Provided by: hughc7
Category:

less

Transcript and Presenter's Notes

Title: Bitwise Operations and Miscellaneous Topics


1
Bitwise Operations andMiscellaneous Topics
  • CS-2301, System Programming for Non-majors
  • (Slides include materials from The C Programming
    Language, 2nd ed., by Kernighan and Ritchie and
    from C How to Program, 5th ed., by Deitel and
    Deitel)

2
Topics for Today
  • Bitwise Operations
  • Discussion of Homework 4
  • (A little bit of) insight into Operating Systems

3
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

4
Bitwise Operations
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 are different
  • Complement
  • Each bit is reversed
  • ltlt Shift left
  • Multiply by 2
  • gtgt Shift right
  • Divide by 2

5
Examples
  • unsigned int c, a, b
  • c a b
  • c a b
  • c a b
  • b a
  • c a ltlt 2
  • b a gtgt 3

6
Example Printer Status Register
Clean
Low ink
Paper jam
Empty paper
  • Traditional C definition of bit fields
  • define EMPTY 01
  • define JAM 02
  • define LOW_INK 16
  • define CLEAN 64

7
Example Printer Status Register (cont.)
Clean
Low ink
Paper jam
Empty paper
  • 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 /

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 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

9
Example Printer Status Register (cont.)
Clean
Low ink
Paper jam
Empty paper
  • An integer field (traditional style)
  • define COUNT (8163264128)
  • int c (status COUNT) gtgt 3
  • status (c ltlt 3) COUNT

10
Alternative Bit-field Definitions
Clean
Low ink
Paper jam
Empty paper
  • struct statusReg unsigned int emptyPaperTray
    1unsigned int paperJam 1
    2unsigned int lowInk
    1 1unsigned int
    needsCleaning 1
    1

11
Example Printer Status Register (cont.)
Clean
Low ink
Paper jam
Empty paper
  • struct statusReg unsigned int emptyPaperTray
    1unsigned int paperJam 1
    1unsigned int count
    5 1unsigned int
    lowInk 1
    1unsigned int needsCleaning 1
    1

12
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

13
Warning
  • Almost everything about bit fields is
    implementation dependent.
  • Especially the order of fields in the struct!
  • Consult your hardware and compiler implementation!

14
Questions about Bit Fields?
15
Revisit Homework 4
  • 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

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

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

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

19
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

20
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

21
Application to Homework 4
  • int main(int argc, char argv)
  • for(i 1 i lt argc i) if (argvi '-')
    // Process Command line switches
  • else
  • // Open File // ReadAndPrint file with
    width tab // close file
  • //for

22
Application to Homework 4 (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

23
Application to Homework 4 (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

24
Application to Homework 4 (continued)
  • int JustifyPrint(char s, int width, int tab)
  • while(not at end of string s) // scan to
    end of one line // expand tabs while scanning
    // figure out where word break is
  • // print the line

25
Application to Homework 4 (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

26
Application to Homework 4 (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

27
Homework 4 (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 itab
  • if (s '\t') do linei ' 'while (itab
    ! 0)
  • Need to copy as many characters as fit in a line

28
Homework 4 (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

29
Stepwise Refinement for Homework 4
  • 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.

30
Stepwise Refinement for Homework 4
  • 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 didnt work out on first 2-3
    attempts

31
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 dont fit at end of line
  • And pass them to next line

32
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

33
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

34
Stepwise Refinement (again)
  • First attempts at top-down approach were wrong!

35
Application to Homework 4 (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

36
Application to Homework 4 (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

37
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

38
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!

39
Application to Homework 4 (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

40
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

41
GetOneLine() (continued)
  • int GetOneLine(char s, char line, int n, ...)
    int lp, sp nfor(lp 0 ssp!'\0'
    lpltmaxLen sp) if (ssp '\t') do
    linelp ' ' while (lptab ! 0)
    else linelp sspif
    (ssp'\0') linelp '\0' return
    spelse
  • // scan backwards to end of last word// set
    linelp '\0'// scan forward to next
    non-blank, return sp

42
GetOneLine() (continued)
  • int GetOneLine(char s, char line, int n, ...)
    int lp, sp nfor(lp 0 ssp!'\0'
    lpltmaxLen sp) if (ssp '\t') do
    linelp ' ' while (lptab ! 0)
    else linelp sspif
    (ssp'\0') linelp '\0' return
    spelse
  • // scan backwards to end of last word// set
    linelp '\0'// scan forward to next
    non-blank, return sp

Expand tabs!
43
GetOneLine() (continued)
  • int GetOneLine(char s, char line, int n, ...)
    int lp, sp nfor(lp 0 ssp!'\0'
    lpltmaxLen sp) if (ssp '\t') do
    linelp ' ' while (lptab ! 0)
    else linelp sspif
    (ssp'\0') linelp '\0' return
    spelse
  • // scan backwards to end of last word// set
    linelp '\0'// scan forward to next
    non-blank, return sp

Copy character!
44
GetOneLine() (continued)
  • int GetOneLine(char s, char line, int n, ...)
    int lp, sp nfor(lp 0 ssp!'\0'
    lpltmaxLen sp) if (ssp '\t') do
    linelp ' ' while (lptab ! 0)
    else linelp sspif
    (ssp'\0') linelp '\0' return
    spelse
  • // scan backwards to end of last word// set
    linelp '\0'// scan forward to next
    non-blank, return sp

Test for end of paragraph!
45
GetOneLine() (continued)
  • int GetOneLine(char s, char line, int n, ...)
    int lp, sp nfor(lp 0 ssp!'\0'
    lpltmaxLen sp) if (ssp '\t') do
    linelp ' ' while (lptab ! 0)
    else linelp sspif
    (ssp'\0') linelp '\0' return
    spelse
  • // scan backwards to end of last word// set
    linelp '\0'// scan forward to next
    non-blank, return sp

46
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

47
Discussion or Questions?
Write a Comment
User Comments (0)
About PowerShow.com