CSCI 515 C/C++ Programming Fall 2001



CSCI 515 C/C++ Programming Fall 2002

Dr. Creider

CLab 1 Assignment (In lab class)

Calculating powers of two using multiplication and bit shifting

Write a program to compute powers of two that will fit in unsigned long using two different methods: multiplication and bit shifting.

Write a main program to enter a value to compute a power of two. Make the user to reenter the value if the number entered would create a power of two that would cause overflow in an unsigned long variable used to store the result. Call two separate functions each of which will compute a power of two. You cannot use the “pow” function in this program. Print the value computed by each function and continue entering new values to compute powers of two until the user enters a negative number.

Write a function to compute a power of two using multiplication. Initialize the result (unsigned long) to one (1) and multiply the previous value of result by two in a loop until you have computed the correct answer. Pass to the function the power of two value to compute and pass the variable to be assigned the result of the calculation by reference.

Write a function to compute a power of two using bit shifting. Compute the result (unsigned long) by shifting 1 to the left the number of times of the power to two to be computed. For example if you wanted to compute 210 you would write the statement: result = 1 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches