Josh's notes - Andrew Cmu

5 downloads 71 Views 148KB Size Report
15-122, Recitation 3: Bit Manipulations, Arrays, and For Loops. Cosku Acay. 1 Bit Manipulations. Bitwise operations operate on integers one bit at a time: they ...
15-122, Recitation 3: Bit Manipulations, Arrays, and For Loops

1

Cosku Acay

Bit Manipulations

Bitwise operations operate on integers one bit at a time: they treat all bits in a number as independent units that do not have anything to do with each other. The four basic bitwise operators in C0 are and (&), or (|), xor (ˆ), and not (∼). We can use these operations to get, set, or invert certain bits. Example 1.1. Draw the product tables for these four operations. Based on these, try to identify the use of each operator (getting, setting, or inverting). Solution:

Example 1.2. Write a function that clears (sets to 0) the alpha and green channels. Your function should only use the operator &. 1 2 3 4 5 6

typedef i n t pixel ; i n t redAndBlue ( pixel p ) // @ensures \ result & 0 xff00ff00 == 0; { return ; }

Example 1.3. Write a function that removes transparency (sets the alpha channel to 0xFF). 1 2 3 4 5 6

typedef i n t pixel ; i n t re mo ve Tr an sp ar en cy ( pixel p ) // @ensures (\ result >> 24) & 0 xff == 0 xff ; { return ; }

Example 1.4. Write a function that flips the s-th most significant bit of n. 1

15-122, Recitation 3: Bit Manipulations, Arrays, and For Loops 1 2 3 4 5

i n t flip ( i n t n , i n t s ) // @requires 0 = 0; // @ensures \ result == n % 8; { return }

;

Example 2.5. Write a multiplication by 5 function that does not use the ∗ operator. 1 2 3 4 5

i n t multiplyByFive ( i n t n ) // @ensures \ result == n * 5; { return }

3

;

Arrays

Arrays are stored as pointers in C0 . When you have a variable representing an array, it is stored as a reference, not a value. So, if you pass an array to a function and modify it within the function, the caller of the function will have the modified version. Example 3.1. The following code “allocates” an array A, assigns its value to B, and modifies A. What will be the return value of the function? Keep in mind that arrays are references, not values.

3

15-122, Recitation 3: Bit Manipulations, Arrays, and For Loops

Cosku Acay

1 i n t reference () 2 { 3 i n t array_size = 1; 4 i n t [] A = alloc_array ( int , array_size ) ; 5 A [0] = 15; 6 7 i n t [] B = A ; 8 A [0] = 122; 9 return B [0]; 10 }

Solution:

It is important to make sure that all array accesses are in bounds. Ideally, we should be able to prove that they are. Almost every for loop that accesses an array must have a loop invariant justifying that the access is safe. Example 3.2. Add the necessary loop invariant to ensure that array accesses are safe. 1 2 3 4 5 6 7 8 9 10 11

i n t addOne ( i n t [] arr , i n t size ) // @requires size = size ; // @requires ( rowSize > 0) && ( size % rowSize == 0) ; // @ensures \ length (\ result ) == size / rowSize ; { i n t nRows = ; i n t [][] B = alloc_array ( i n t [] , nRows ) ; i n t k = 0; f o r ( i n t i = 0; i < nRows ; i ++) // @loop_invariant { B [ i ] = alloc_array ( int , f o r ( i n t j = 0; j < rowSize ; j ++) // @loop_invariant { B[ ][ ] = A [ k ]; k ++; } } return B ;

; ); ;

}

Example 5.3. We represent an image with a one-dimensional array of pixels, a width, and a height. Write a function that sets the alpha channel of every pixel in a given image to 0xFF. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

typedef i n t pixel ; typedef pixel [] Image ; Image opaque ( Image A , i n t width , i n t height ) // @requires width > 0 && height > 0; // @requires \ length ( A ) >= width * height ; // @ensures \ length (\ result ) == \ length ( A ) ; { f o r ( i n t i = 0; // @loop_invariant {

} return A ; }

8

; i ++) ;

15-122, Recitation 3: Bit Manipulations, Arrays, and For Loops

6

Cosku Acay

Loop Invariants Beyond Ensuring Safe Access

Here is another interesting use of loop invariants to make statements about your array as you are traversing and modifying it. Consider the following functions: 1 2 3 4 5 6 7 8 9 10 11

/* checks that A [0] through A [n -1] are true */ bool all_true ( bool [] A , i n t n ) // @requires 0