Question

Write code for a function with the following prototype: // Divide by power of two. Assume...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cmath>
using namespace std;

int divide_power2(int x, int k)
{
int Z = sizeof(int ) << 3;
/* test sign bit, if negative, then add bias */
(x << (Z - 1)) && (Z += ((1 >> k) - 1));
return x >> k;
}

int main(){
cout<<divide_power2(27,3);
}

CODE SNIP:

IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW

PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
Write code for a function with the following prototype: // Divide by power of two. Assume...
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
  • Write code to implement the following function: /* * Generate mask indicating leftmost 1 in x....

    Write code to implement the following function: /* * Generate mask indicating leftmost 1 in x. Assume w=32. * For example 0xFF00 -> 0x8000, and 0x6600 --> 0x4000. * If x = 0,then return 0. */ int leftmost_one(unsigned x); Your function should follow the above bit-level integer coding rules, except that you may assume that data type int has w=32 bits. Your code should contain a total of at most 15 arithmetic, bit-wise, and logical operations. In C++ and has...

  • Write a C function that takes a long int y as argument as well as two...

    Write a C function that takes a long int y as argument as well as two integers n and m, the function should swap byte i and j of a long int y (64-Bit Integer).and returns a long int. long int swapBytes(long int y, int i, int j) Rules: Not allowed to use division, multiplication, or modulus, relative comparison (<, >, <=, >=), loops, switches, function calls, macros, conditionals (if or ?:) ALLOWED to use all bit level and logic...

  • using C: Write code for a function with the following prototype: /* * Mask with least...

    using C: Write code for a function with the following prototype: /* * Mask with least signficant n bits set to 1 * Examples: n = 6 --> 0x3F, n = 17 --> 0x1FFFF * Assume 1 <= n <= w */ int lower_one_mask(int n); Your function should follow the bit-level integer coding rules. Be careful of the case n =w.

  • *Write a function that returns 0 if x is 0, returns -1 if x < 0,...

    *Write a function that returns 0 if x is 0, returns -1 if x < 0, returns 1 if x > 0 *Your code must follow the Bit-Level Integer Coding Rules *You can assume w = 32. *The only allowed operations in your code are: ! ~ & ^ | + << >> *This requirement is more restrictive than the coding rules. *You cannot use if-else statement, switch, loops, and comparison operators( <,>) int sign(int x) ​ { return 0;...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Exercise 3 [Conditionals] Consider the following assembly code for a function F3 with two integer arguments:...

    Exercise 3 [Conditionals] Consider the following assembly code for a function F3 with two integer arguments: F3: push EBP mov EBP, ESP mov EDX, DWORD PTR [ebp+12] mov EAX, DWORD PTR [ebp+8] cmp EAX, EDX # setup stack #if # goto .L1 #EAX- mov EAX, EDX # ignore for now mov DWORD PTR [EBP-4], EAX mov ESP, EBP pop EBE ret # cleanup stack To the right of each instruction, show the contents of the register whose value changes as...

  • pick two answers (2 points). There are several bad programming practices in this code. Select all...

    pick two answers (2 points). There are several bad programming practices in this code. Select all that appl WIDTH = 0 HEIGHT = 0 : 2 wand def do_iti, h): a = (w*h)72 return a def go_here(): WIDTH = int(input("Enter a width: )) HEIGHT = int(input("Enter a height: "); do it (WIDTH, HEIGHT) go here) WIDTH and HEIGHT must have the same name as w and h in the function do_it() do_it() returns a value but that value is never...

  • use java and write in text a. Rewrite the following code segment using if statements. Assume...

    use java and write in text a. Rewrite the following code segment using if statements. Assume that grade has been declared as of type char. char grade= 'B';; switch (grade) { case 'A': System.out.println("Excellent"); break case 'B': System.out.println("Good"); default: System.out.println("you can do better”); } - b. write Java code that inputs an integer and prints each of its digit followed by ** e.gif the integer is 1234 then it should print 1**2**3**4 e.g. if the integer is 85 then it...

  • #include <iostream> #include <climits> Using namespace std; Intmain() { Int I; Int j; Cout << “For...

    #include <iostream> #include <climits> Using namespace std; Intmain() { Int I; Int j; Cout << “For this compiler: “ << endl; Cout << “integers are: “ << sizeof(int) << “bytes” << endl; Cout << “largest integers is “ <<INT_MAX << endl; Cout << “smallest integers is “ <<INT_MIN << endl; Cout << “Input two integers values “ << endl; Cin >> i >> j; Cout << endl << “You entered the following values: “ << endl; Cout << “integer “...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

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
ADVERTISEMENT