Question

C++ Suppose we are given the task of generating code to multiply integer variable x by...

C++

Suppose we are given the task of generating code to multiply integer variable x by various different constant factors K. To be efficient, we want to use only the operations +, -, and <<. For the following values of K, write C expressions to perform the multiplication using at most three operations per expression.

A. K = 17: B. K =− 7: C. K = 60: D. K =− 112:

Must obey bit integer coding rules:

  • Assumptions
    • Integers are represented in two's-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) << 3 to compute w.
  • Forbidden
    • Conditionals (if or ?:), loops, switch statements, function calls, and macro invocations.
    • Division, modulus, and multiplication.
    • Relative comparison operators (<, >, <=, and >=).
    • 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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

(A)K=17
K = 16 + 1
X*K = X*(16) + X
X*K = (X << 4) + X

C expression
X = (X << 4) + X

(B)K=-7
K = 1 - 8
X*K = X - X*8
X*K = X - (X<<3)

C expression
X = X - (X << 3)

(C) K=60
K = 64 - 4
X*K = X*64 - X*4
X*K = (X << 6) - (X << 2)
C expression
X = (X << 6) - (X << 2)

(D) K=-112
K = 16 - 128
X*K = X*16 - X*128
X*K = (X << 4) - (X << 7)
C expression
X = (X << 4) - (X << 7)

Add a comment
Know the answer?
Add Answer to:
C++ Suppose we are given the task of generating code to multiply integer variable x by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT