Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (anonymous):

divpwr2 - Compute x/(2^n), for 0 <= n <= 30 * Round toward zero * Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2 * Legal ops: ! ~ & ^ | + << >> * Max ops: 15 Can someone help me out with this please

OpenStudy (e.mccormick):

Well, in what language and what did you do so far?

OpenStudy (anonymous):

In C language ... i tried to use loops to finish it but my professor said i cannot use that in this as a coding rule ..

OpenStudy (e.mccormick):

I do not see why you would need a loop. x/(2^n) \(\leftarrow\) That is the calculation you need for 0 <= n <= 30 \(\leftarrow\) That is the valid input for n, so you just need to make sure n does not fall outside that domain. Round toward zero \(\leftarrow\) simple enough. Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2 \(\leftarrow\) OK, so lets look: divpwr2(x,n) divpwr2(15,1) = 15/(2^1) = 15/2 = 7.5 which rounds to 7 divpwr2(-33,4) = -33/(2^4) = -33/16 = -2.0625 which rounds... hmmm... They are saying it is -2 but tat does not seem right. To me, "towards 0" would be round down above 0 and round up below 0. But it looks like they are just using round off.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!