Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Binghamton University Exam 1 Review CS220. 2 Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer.

Similar presentations


Presentation on theme: "1 Binghamton University Exam 1 Review CS220. 2 Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer."— Presentation transcript:

1 1 Binghamton University Exam 1 Review CS220

2 2 Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer representation, conversion, casting, arithmetics… Floating point representation, covnersion, casting, arithmetics, rounding,... The C programming Language  Pointers  Arrays and pointer arithmetics  Dynamic memory allocation  Structs  Data structures such as linked lists Chapter 2 in the book, and the C tutorial on the website

3 3 Binghamton University Concerned about C; major concepts Basic Syntax:  Similar to Java (no objects, and Java has no pointers)  C++ is a superset of C (C has no classes, templates, etc…)  I assumed you know basic syntax  if this is a wrong assumption, please let me know  Module 1 in tutorial (self-study) has some help Pointers are variables that hold an address  They have a type, representing the type stored at the memory address  & and *  Pointer arithmetic in units of the type  Can be used to get around C’s pass by value

4 4 Binghamton University C major concepts (cont’d) Arrays are allocated as a contiguous block of memory  Elements are stored in order  Row major for multi-dimensional arrays  Use indices to compute memory location of element  Can use pointers to access the array elements Strings are arrays of characters terminated by a \0 (NULL character) Multi-dimensional arrays as arrays of pointers are different; see the examples in the slides

5 5 Binghamton University C Major concepts, continued Two types of variables: those allocated by compiler, and those allocated by programmer dynamically  Static memory: when you declare a variable, or an array  compiler allocates space and manages (usually on the stack)  You don’t need to free it  Have to provide size at compile time; cannot change it later  Dynamic memory: Alternatively, when you use malloc, you dynamically ask for a chunk of memory (on the heap)  You manage it manually

6 6 Binghamton University C Major concepts; continued Structs are a composition of variables (kind of a degenerate object) Assignment of a struct to another causes a shallow copy Can nest structs as long as there is no loop Can include pointers to structs as members  including to structs of the same type Can treat them as any other variable: e.g., allocate arrays of structs, malloc them, etc… Should get familiar with use to build data structures such as linked lists Lets do some problems!

7 7 Binghamton University Integer C Puzzles x < 0  ((x*2) < 0) ux >= 0 x & 7 == 7  (x<<30) < 0 ux > -1 x > y  -x < -y x * x >= 0 x > 0 && y > 0  x + y > 0 x >= 0  -x <= 0 x = 0 (x|-x)>>31 == -1 ux >> 3 == ux/8 x >> 3 == x/8 x & (x-1) != 0 int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y; Initialization

8 Problems Consider a 6-bit machine, which uses 2s complement for negative numbers. Assume shorts are represented using 3 bits. short sy = -3; int y = sy; Int x=-17; unsigned ux=x; Fill in theTable: ExpressionDecimalBinary Zero0 --6 -010010 ux y x>>1 TMax -TMin Tmin+TMin

9 Consider an 8-bit floating point number based on IEEE floating point format. There is a sign bit, 3 exponent bits, with bias of 3, and 4 bits of fraction. The rules are like those in the IEEE standard (normalized, denormalized, representation of 0, infinity and NaN). The number has the form V=(-1) s x M x 2 E Fill in the table below (Binary is the 8-bit binary representation, M is the significand, E is the exponent, and V is the numeric value). DescriptionBinaryMEValue Minus zero-0.0 -0 100 0101 Smallest denormalized Largest normalized One1.0 -5.5 Positive infinity--+∞

10 Consider a 12 bit floating point representation based on the IEEE floating point format. There is one sign bit, 4 exponent bits, and 7 significand bits. (a) How many floating point numbers are in the following intervals [a,b)? For each interval, count the numbers of x such that a ≤ x < b (a) Interval [1,2) (b) Interval [2,3) (b) For denormalized numbers, what is the value E of the exponent after biasing? What is the largest value M of the significand? (c) For normalized numbers, what is the smallest value E of the exponent after biasing? What is the largest value E of the exponent after biasing? What is the largest value M of the significand?

11 Some C problems General  Make sure you understand quiz; will make key available  K&R is an excellent source of practice problems  Tutorial links for exercises. Some more in following slides. Will make more available this evening and tomorrow.

12 Look at the following code segments. If they produce an output, provide it. If they have an error, clearly identify it and discuss its effect. int main(int argc, char* argv[] ){ int x = 10; int* ptr = &x; ptr++; printf(“%x %d \n”, ptr, *ptr); }

13 Study the following code segment. What is the purpose of the foo function? Does it work as intended? If not, how would you fix it? Show any memory leaks. int n=10; int main() { int ** A=malloc(sizeof(int *)*n); //could I use int * A[n]; instead? … foo(&A); n*=2; … } int foo(int ***array){ int **arrayint = (int**)malloc(2*n*sizeof(int *)); for(i=0; i<n;i++) arrayint[i]=(*array)[i]; array=&arrayint; }

14 Consider the following code: int A[]={1,2,3,4,5}; int * ptr = A + 5; int *ptr2 = A; What are the values of the following? If undefined say why: (a) *ptr (b) *(ptr-3) (c) ptr-ptr2 (d) *(ptr-1)+2 (e) &ptr

15 Consider the following code int A[2][3] = {{1,2,3},{4,5,6}} and assume that A[0]=0xFFAA0000 What is the value of the following? (a) A[1] (b) *(A[1]) (c) *(A[0]+1)

16 A linked list that ends with a self-loop (the last element pointing to itself) is called a sloop. Alternatively, linked lists could end with a null pointer. Given a linked list made up of the following struct elements: typedef struct node { int data; struct node *next; } node; Write a function that returns TRUE if the linked list is a sloop, otherwise false. Assume that there are no other loops in the linked list.

17 The following function (addend) is supposed to add a node to the end of a sloop. It must retain the sloop property. However, the code contains some bugs. Find the bugs and fix them by rewriting or adding new statements. node * addend(node *head,int data) { node * q = malloc (sizeof(node)); while(head != head->next) head=head->next; head->data=data; head->next=q; }


Download ppt "1 Binghamton University Exam 1 Review CS220. 2 Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer."

Similar presentations


Ads by Google