Question

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 inver

Using lenguague C

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

Code:-

#include<stdio.h>
int toggleKthBit(int n, int k)
{
return (n ^ (1 << (k-1)));
}
int setbits(int x,char p,char n)
{
   int i;
   for(i=0;i<n-'0';i++)
   {
       x=toggleKthBit(x,p-'0'+i+1);
   }
   return(x);
}
int main()
{
   int x=18;
   char p='1';
   char n='3';
  
   printf("New value of %d is %d",x,setbits(x,p,n));
}

Sample Output:-

F:Codes Untitled2.exe New value of 18 is 28 Process exited after 0.6631 seconds with return value 21 Press any key to continu

As x=18 which is 10010 in binary.

p=1 and n=3;

So,the result is 11100 which is 28.

Add a comment
Know the answer?
Add Answer to:
Using lenguague C E5.5 (página 239) Write a function setbits(int x.char p.char n) that returns 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
  • Need help about C programming! Inverting binary! Write a function, invert(x, p, n) that returns x...

    Need help about C programming! Inverting binary! Write a function, invert(x, p, n) that returns x with the n bits that begin at position p inverted, leaving the others unchanged. For example, if x is 0b1001, n=1 and p=1, invert(x, p, n) would return "0b1011". We can see that 1 bit at position 1 was inverted. Another example, if x is 0b11001011, n=3 and p=2, invert(x, p, n) would return "0b11010111". Thanks!

  • (a) Write a recursive function int find(const int A[], int n, int x); which returns the...

    (a) Write a recursive function int find(const int A[], int n, int x); which returns the first index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found. (b) Write a recursive function int rfind(const int A[], int n, int x); which returns the last index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found....

  • In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an...

    In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an array (allocated off of the heap in the function) containing only the elements of array a that pass the “test” t. The function iterates through all the elements of a, and constructs a new array containing all the elements that pass the test. When the parameter corresponding to “size” is passed in (by reference), it contains the size of the array a. In the...

  • 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 C program!! Q2 and Q3 Write the following functioned int search(int a[], int n,...

    write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n, int key, int **loc); a is an array to be searched, n is the number of elements in the array, key is the search key, and loc is a pointer to the first location of the search key in array a (if found) or NULL otherwise. The function returns 1 if key is found in a, and returns 0 otherwise. Write a main() and...

  • Please complete the implementation of the four functions IN C. //************************************************************************************/ // // countNumberofOnes // //...

    Please complete the implementation of the four functions IN C. //************************************************************************************/ // // countNumberofOnes // // Description: Counts the number of 1s in an integer passed as argument // Preconditions:input argument is passed as a pointer // Postconditions:the number of 1s returned // // Calls: N/A // Called by: main // //***********************************************************************************/ int countNumberofOnes(uint32_t *intData) { // Write a function that counts number of 1s in an integer passed } //***********************************************************************************/ //* //* setBit //* //* Description: The function sets...

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

  • 13. Write a recursive function with the declaration: int count Equal (int* numbers, int n, int...

    13. Write a recursive function with the declaration: int count Equal (int* numbers, int n, int x) that has as parameters an array numbers with n > 0 elements, and an integer x, and returns how many times I appears in the array. 14. Write a recursive function with the declaration: double dist(double* u, double *v, int n) that gets two double precision arrays with n > 1 elements and returns the value: Vu[0] - v[0])2 + (u[1] – v[1])2...

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

  • C++ ONLY Write a function, int flip(string a[], int n); It reverses the order of the...

    C++ ONLY Write a function, int flip(string a[], int n); It reverses the order of the elements of the array and returns n. The variable n will be greater than or equal to zero. Example: string array[6] = { "a", "b", "", "c", "d", "e" }; int q = flip(array, 4); // returns 4 // array now contains: "c" "" "b" "a" "d" "e" Write a function, int flip(string a[], int n); It reverses the order of the elements of...

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