Write code for a function with the following prototype: // Divide by power of two.
View the step-by-step solution to:

Question

Write code for a function with the following prototype:

// Divide by power of two. Assume 0 <= k <

w-1

int divide_power2(int x, int k);

The function should compute x/2 k with correct rounding (round toward 0), and it should follow the bit-level integer coding rules:


• Assumptions

- Integers are represented in twos-complement form.

- Right shifts of signed data are performed arithmetically.

- Data type int is w bits long. For some of the problems, you will be given a specific value for w, but otherwise your code should work as long as w is a multiple of 8. You can use the expression sizeof(int), =).

• Not allowed

- Conditionals (if or ?:), loops, switch statements, function calls, and macro invocations.

- Division, modulus, and multiplication

- Relative comparison operators (, =).

- Casting, either explicit or implicit.

• Allowed operations

- All bit-level and logic operations.

- Left and right shifts, but only with shift amounts between 0 and w − 1.

- Addition and subtraction.

- Equality (==) and inequality (!=) tests. (Some of the problems do not allow these.)

- Integer constants INT_MIN and INT_MAX.


Please properly document the code and what any significant lines perform

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes