Question

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.

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

Program

#include <stdio.h>

int lower_one_mask(int n)
{
int bits_mask = (n == sizeof(int) << 3) - 1;
int m = ~(bits_mask << n);
return m;
}

int main(int argc, char **argv)
{
printf("n=6 ---> 0X%X\n", lower_one_mask(6));
printf("n=17 ---> 0X%X\n", lower_one_mask(17));
printf("n=32 ---> 0X%X\n", lower_one_mask(32));
}

Output

Add a comment
Know the answer?
Add Answer to:
using C: Write code for a function with the following prototype: /* * Mask with least...
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 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...

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

  • answer in c++ Using the table below, complete the C++ code to write the function prototype...

    answer in c++ Using the table below, complete the C++ code to write the function prototype statement, and the void function header. The void function should receive three double variables: the first two by value and the last one by reference. Name the formal parameters num1, num2 and answer. The function should divide the num1 variable by the num2 variable and then store the result in the answer variable. Name the function calcQuotient. Also write an appropriate function prototype for...

  • Write the C-code for a function which returns the least common character in an set of...

    Write the C-code for a function which returns the least common character in an set of characters that occurs at least once. (The function's prototype is char least_common(int, unsigned char*);)

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

  • C program help 4. Write a function called scan_hex. Here is its prototype: void scan_hex(int *xptr);...

    C program help 4. Write a function called scan_hex. Here is its prototype: void scan_hex(int *xptr); Here is an example of its use, in conjunction with print_decimal. Assume that the user will type a non-negative hex number as input. Also, assume that the “digits” a-f are in lower case. Let’s say the user types 1b int x; scan_hex(&x); print_decimal(x); MODIFY THIS CODE // function inputs a non-negative integer and stores it into *xptr. // The format of the input is...

  • Question 14; Write a C function named isSymmetric, the prototype of which is given below, that...

    Question 14; Write a C function named isSymmetric, the prototype of which is given below, that returns 1 if the elements of an array of integers named myArray of size n are symmetric around the middle. If the array elements are not symmetric, the function should return 0. Both the array and its size are specified as parameters. (10 marks) Clue: Array myArray of size n is symmetric if myArray[0] is equal to myArray[n-1], myArray[1] is equal to myArray[n-2], and...

  • I need this in C++ Write a function using the following structure and prototype. struct Stats...

    I need this in C++ Write a function using the following structure and prototype. struct Stats { float avg; //Average value of an integer array float median; //Median value of an integer array int *mode; //array containing the modes int nModes; //number of modes in the array int maxFreq; //max frequency of modes }; Stats *avgMedMode(int *,int); The function takes in an integer array and the size of the array. Then returns a pointer to a structure containing the average,...

  • Using lenguague C E5.5 (página 239) Write a function setbits(int x.char p.char n) that returns x...

    Using lenguague C E5.5 (página 239) Write a function setbits(int x.char p.char n) that returns x with the n bits that begin at position p inverted (ie 1 changed to O and vice versa). Consider the position 0 the least significant bit of the variable x (remember that x is an integer)

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