4.5 Bitwise Operators - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

4.5 Bitwise Operators

Description:

C_2 (4 ) X. Liu 2004. 4.7 The Increment ... X. Liu 2004. 4.8 Difference between k and k. Example 1: k=1; ... X. Liu 2004. 4.12 The break Statement ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 41
Provided by: ceb245elec
Category:

less

Transcript and Presenter's Notes

Title: 4.5 Bitwise Operators


1
  • 4.5 Bitwise Operators
  • AND
  • Inclusive OR
  • Exclusive OR
  • Right Shift
  • Ones Complement (unary)
  • Examples

2
  • 4.6 Basic File I/O
  • Essentials of a file
  • Example
  • - read data from a file data_in
  • - write data to a file data_out
  • include
  • include
  • main()
  • float var1, var2
  • int var3
  • double var4
  • FILE f1, f2
  • f1 fopen ("data_in", "r")
  • f2 fopen ("data_out", "w")
  • fscanf (f1, "f, f", var1, var2)

3
file data_in 123.4, 2.3456 10,
0.234567 file data_out sum var1 var2
125.7456 var3 10 var4 0.234567
  • In general, fpfopen(name, mode)
  • mode can be r, w, a
  • (a means append)
  • Opening an existing file with w causes the old
    data to be discarded
  • With a, the old data is still there
  • Trying to read a non-exist file is an error

4
  • 4.7 The Increment and Decrement operators , --
  • k or k means k k1
  • k-- or --k means k k-1
  • Example 1a
  • int k1
  • k
  • Result k is now equal to 2
  • Example 1b
  • int k1
  • k
  • Result k is now equal to 2

5
  • Example 2a
  • int k4
  • k--
  • Result k is now equal to 3
  • Example 2b
  • int k4
  • --k
  • Result k is now equal to 3

6
  • 4.8 Difference between k and k
  • Example 1
  • k1
  • i10(k) / After the operation, i is 10 /
  • jk / now j is 2 /
  • k is incremented after the expression is
    evaluated
  • i.e.
  • i10(k) means
  • i10k kk1

7
  • Example 2
  • k1
  • i10(k) / After the operation, i is 20 /
  • jk / now j is 2 /
  • k is incremented before the expression is
    evaluated
  • i.e.
  • i10(k) means
  • kk1 i10k
  • 4.9 Difference between k- - and - -k
  • Similar to

8
  • 4.10 The while Loop
  • General Format
  • while(expression)
  • statements ...
  • Flow chart
  • Example 1
  • float x 0.0, sum 0.0
  • while(x0)
  • sum sum x
  • scanf(f,x)
  • printf(5.2f\n, sum)
  • Input
  • 1.0
  • 0.5
  • 1.0

9
Example 2 float x 0.0, sum 0.0
while(x0) scanf(f,x) sum
sum x printf(5.2f\n,
sum) Input 1.0 0.5
1.0 -1.0 Output 1.5
10
  • 4.11 The do-while Loop
  • General Format
  • do
  • statements ...
  • while(expression)
  • Flow chart

11
  • Example 1 int k, count 0
  • do scanf(d,k)
  • if( k2 ) count
  • while (k)
  • printf(d\n,count)
  • Input Output
  • 1 4
  • 2
  • 3
  • 2
  • 2
  • 4
  • 2
  • 0

12
  • 4.12 The break Statement
  • When the break statement is encountered within a
    loop, the loop is terminated immediately.
  • Example
  • sum 0
  • for(k0 k
  • scanf(d,i)
  • if(i10) break
  • sum sum i
  • printf(d\n,sum)
  • Input Output
  • 1 6
  • 2
  • 3
  • 15

13
  • 4.13 The continue Statement
  • Do the next iteration, skipping the remaining
    statements.
  • Example
  • sum 0
  • for(k0 k
  • scanf(d,i)
  • if(i10) continue
  • sumsumi
  • printf(d\n,sum)
  • Input Output
  • 1 11
  • 2
  • 3
  • 15
  • 5

14
4.14 Nested loops - Example / SYEN 4385 Example
program to illustrate nested loops. This
program evaluates numerically the derivative of
log(x) for x1,2,...,10 using central difference
scheme with h1/2,1/4,1/8,1/16, .... /
15
include include main()
int i, j float x, dfdx, h i 1
while(ix1.0i printf("5.1f ",x)
for(j1jevaluate derivative / dfdx
(log(xh)-log(x-h))/(2h) printf("13.3e
",dfdx) i printf("\n")
16
Nested loops - example OUTPUT 1.0
1.099e00 1.022e00 1.005e00 1.001e00 2.0
5.108e-01 5.026e-01 5.007e-01 5.002e-01 3.0
3.365e-01 3.341e-01 3.335e-01 3.334e-01 4.0
2.513e-01 2.503e-01 2.501e-01 2.500e-01 5.0
2.007e-01 2.002e-01 2.000e-01 2.000e-01 6.0
1.671e-01 1.668e-01 1.667e-01 1.667e-01 7.0
1.431e-01 1.429e-01 1.429e-01 1.429e-01 8.0
1.252e-01 1.250e-01 1.250e-01 1.250e-01 9.0
1.112e-01 1.111e-01 1.111e-01 1.111e-01 10.0
1.001e-01 1.000e-01 1.000e-01 1.000e-01
17
  • 4.15 The switch Statement
  • General Format
  • switch(variable)
  • case constant_1
  • statements
  • break
  • case constant_2
  • statements
  • break
  • case constant_N
  • statements
  • break
  • default
  • statements

18
  • The switch statement is used to select one of
    several alternative paths in program execution,
    based on the value of a single variable.
  • If the variable matches constant i (which can
    be int or char type), the statements between
    constant i and break will be executed
    otherwise, the default statements are executed.

19
  • 5 Data Types
  • Five basic data types
  • int, float, double, char, void
  • Type modifiers
  • signed, unsigned
  • short, long
  • How many combinations? 36?
  • ANSI standard see page 109, Teach Yourself C
    (3rd.), Herbert Schildt
  • HP machine implementation see Table 1 in page
    5, your Notes
  • Also, look at files limits.h and float.h
    under directory /usr/include

20
  • 5-1 int
  • At least 16 bits
  • Machine dependent
  • HP machine 32 bits
  • signed
  • int i
  • signed i
  • signed int i
  • unsigned
  • unsigned i
  • unsigned int i

21
  • To print printf(u, i)
  • To read scanf(u, i)
  • long
  • long int k
  • long k
  • The variable in some other systems has twice the
    byte length as int, but in our HP system long is
    same as int
  • unsigned long int k is same as unsigned int k

22
  • short
  • short int k
  • short k
  • The variable has half the byte length as int
  • HP size 2 bytes
  • To print printf(d, k)
  • To read scanf(d, k)

23
  • unsigned short
  • unsigned short int k
  • unsigned short k
  • HP size 2 bytes
  • To print printf(u, k)
  • To read scanf(u, k)

24
  • 5-2 float
  • HP machine 32 bits
  • float x
  • To print
  • printf(f, x)
  • printf(e, x)
  • printf(g,x)
  • To read
  • scanf(f, x)
  • scanf(e, x)
  • scanf(g, x)
  • If g is used, the computer will choose f or e
    automatically, depending on which results in the
    most compact output

25
  • 5-3 double
  • HP machine 64 bits
  • double x
  • To print
  • printf(lf, x)
  • printf(le, x)
  • printf(lg,x)
  • To read
  • scanf(lf, x)
  • scanf(le, x)
  • scanf(lg, x)

26
  • 5-4 char
  • HP machine 8 bits
  • char s
  • To print
  • printf(c,s)
  • To read
  • scanf(c, s)

27
  • 5-5 Type Casting
  • C allows coercion that is forcing one variable
    of one type to be another type
  • Examples
  • int i float a 1.3
  • i(int)a / i is now 1 /
  • int i3 float a
  • a(float)i / a is now 3.0 /
  • int i char ch'A'
  • i(char)ch / i is now 65 - ASCII code for A /

28
  • 6 Functions
  • You have already seen some C library functions
    such as exp, pow, ....
  • You can also define your own functions
  • 6-1 General Format
  • return_type function_name
  • (parameter1_type parameter1, parmater2_type
    parameter2, ...)
  • statements
  • return(expression)

29
  • return_type double, float, int, char, ...
  • return (expression)
  • terminates execution of the function and
    returns control to the calling function
  • evaluates the expression in ( ), and returns the
    value of the expression to the calling function
  • Examples
  • return (a)
  • return (ab)
  • return (5)
  • You can call any function from main()
  • You can call any function from another function
  • You cannot call main() from other function

30
  • 6-2 An example
  • include
  • float find_average(float, float) / prototype
    /
  • main()
  • float x5, y15, result
  • / x, y are arguments /
  • result find_average(x, y)
  • printf("averagef\n", result)
  • / a, b are parameters, i.e. formal arguments /
  • float find_average(float a, float b)
  • float average
  • average(ab)/2
  • return(average)

31
  • 6-3 Usage of Type void
  • If you do not want to return a value you can use
    the void function (note no return statement is
    requited in this case).
  • Example to print out 1, 4, 9, 16, ...
  • include
  • void squares()
  • main()
  • squares
  • / try to use squares() here to see what will
    happen /
  • void squares()
  • int loop
  • for (loop1 loop
  • printf("d\n", looploop)

32
  • 6-4 Some Features of C
  • 6-4-1 Arithmetic Operator
  • Used for int only
  • Produces the remainder when x is divided by y
  • Recall Integer division truncates any
    fractional part

33
  • 6-4-2 Conditional Expression
  • if(ab)
  • za
  • else
  • zb
  • An equivalent and more efficient way to write
    this is
  • z(ab)? ab
  • Here ? is a ternary operator
  • expression1 ? expression 2 expression3
  • means if expression1 is true, then do
    expression2 otherwise do expression3

34
6-4-3 Assignment Operators
  • Equivalent statements
  • x x 0.5 x 0.5
  • x x - dx x - dx
  • x x 10.0 x 10.0
  • x x/100 x / 100
  • In general
  • expr1 op expr2 means
  • expr1 (expr1) op (expr2)
  • Other examples of op are

35
  • 6-5 Global Variables
  • Be declared outside of all functions (including
    main())
  • Be visible to all functions that follows the
    declaration
  • Values can be changed by these functions

36
  • Example
  • include
  • void fn1()
  • void fn2()
  • int i float x
  • main()
  • i1 x10.0
  • printf(d, f\n, i, x)
  • fn1()
  • printf(d, f\n,i,x)
  • fn2()
  • printf(d, f\n,i,x)
  • void fn1()i x5
  • void fn2()i10 xx

37
  • 6-6 Local Variables
  • Be declared inside a function
  • Be visible only to the function in which they
    are declared
  • Content of a local variable can only be changed
    by the corresponding function
  • Be created upon entry into the function and
    destroyed upon exit

38
  • Example
  • include
  • void fn1()
  • main()
  • int i float x
  • i1 x10.0
  • printf(main1 d, f\n,i,x)
  • fn1()
  • printf(main2 d, f\n,i,x)
  • void fn1()
  • int i float x
  • i2 x15.0
  • printf(fn1 d, f\n,i,x)
  • OUTPUT
  • main11, 10.000000

39
  • 6-7 Static Variables
  • Be local to a particular function
  • Only initialized once during the first call to
    the function
  • The value of a static variable on leaving the
    function remains intact
  • On the next call to the function, the static
    variable still has the same value as the last
    time.

40
  • Example
  • include
  • void stat()
  • main()
  • int i
  • for(i0 i
  • void stat()
  • int nonstat_var0
  • static int stat_var0
  • printf("nonstaticd, staticd\n",
  • nonstat_var, stat_var)
  • nonstat_var
  • stat_var
  • OUTPUT
Write a Comment
User Comments (0)
About PowerShow.com