Question

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 to obey following rules

Write the required code expressions in separate functions. Write the required functions and test with a small main program. All programs should be tested and the output should be pasted below the main program.

Your code must follow these rules:

Assumptions • Integers are represented in two’s-complement form. • Right shifts of signed data are performed arithmetically. • Datatype 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 >=)

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

#include <stdio.h>

int leftmost_one(unsigned x)

{

if(x == 0)

return 0;

// extending the leftmost one to the right end

x = x | (x >> 1);

x = x | (x >> 2);

x = x | (x >> 4);

x = x | (x >> 8);

x = x | (x >> 16);

// then subtract off all the other ones to get the resultant mask

int result = x - (x >> 1);

return result;

}

int main()

{

unsigned x;

printf("Enter x: ");

scanf("%i",&x);

printf("%i\n", leftmost_one(x));

return 0;

}

/*output:

Enter x: 0xFF00

32768

Enter x: 0x6600

16384

*/

Add a comment
Know the answer?
Add Answer to:
Write code to implement the following function: /* * Generate mask indicating leftmost 1 in x....
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 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...

  • 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;...

  • Implement the functionality of "main" function for the following C code in MIPS assembly. Comment your...

    Implement the functionality of "main" function for the following C code in MIPS assembly. Comment your code including which registers represent the variables in the main function. Include the .data and the .text sections. Do not write the bit_count function. int main() { int count = 149; int num = 432; if (count + num > 534){ count = bit_count(num); printf("%d bits to store the number", count); }else{ printf("cannot count bit in %d",) } }

  • Problem 1: Write a function add64 that adds two input unsigned 64 bit integers x and...

    Problem 1: Write a function add64 that adds two input unsigned 64 bit integers x and y and returns the unsigned 64 bit integer sum z, i.e. zx+y. In your main function, you should assume that x, y, and z will be stored in the following 6 registers as follows: x: upper 32 bits in $t1 y: upper 32 bits in St3 z: upper 32 bits in St5 lower 32 bits in $to lower 32 bits in $t2 lower 32...

  • 2. Write a C code with a function to swap the first four bits of an...

    2. Write a C code with a function to swap the first four bits of an 8-bit unsigned number with the last four bits. The main function should include the variable declaration and initialisation using the appropriate data type, the function call and the display of the result in hexadecimal. The parameter(s) should be passed to the function by reference.

  • Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code...

    Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code for 8-bit signed array, 16-bit unsigned array, 16-bit signed array, 32-bit signed array and 32-bit signed array. 2) Fill in all the blanks in Table 1 using your completed code, following the hints provided within the table. 3) Fill in all the blanks in Table 2 using your completed code, following the hints provided within the table. C++ Program #include <stdio.h> #include <iostream> int...

  • I need help implementing the following code without using filehandling and only getchar(); this is the...

    I need help implementing the following code without using filehandling and only getchar(); this is the prompt "The input for your program will be a text file containing a large amount of English. Typically, an English sentence ends with a period (aka, dot). Many years ago, when people used mechanical typewriters, the proper form was to place one space between words in a sentence, but two spaces after the period at the end of the sentence. This rule is no...

  • Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the...

    Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the program just the two functions for my C++ program. I wanted to post the sample code from my class but it won't allow me it's too long. This calculate shall have the following five functionalities: Provide sign extension for a binary number Provide two’s complement for a binary nunber string signed_extension(string b);              // precondition: s is a string that...

  • c++ show code thanks, only use library <iostream> Question 1 Write the following two functions The...

    c++ show code thanks, only use library <iostream> Question 1 Write the following two functions The first function accepts as input a two-dimensional array of integers. It returns two results: the sum of the elements of the array and the average The second function accepts as input a two-dimensional array of integers and integer value V. It returns true if the value V is found in the array, false otherwise. Write a main program that declares and initializes the following...

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