Question

In: Computer Science

C code required * byteSwap - swaps the nth byte and the mth byte * Examples:...

C code required

* byteSwap - swaps the nth byte and the mth byte
* Examples: byteSwap(0x12345678, 1, 3) = 0x56341278
* byteSwap(0xDEADBEEF, 0, 2) = 0xDEEFBEAD
* You may assume that 0 <= n <= 3, 0 <= m <= 3
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 25
* Rating: 2
*/

Solutions

Expert Solution

#include <stdio.h>

int byteSwap(int x, int n, int m) {
        // offset is simply bitPosition for bytes..
        // So byte 1 means bit position 8.
        int nOffset = n << 3;
        int mOffset = m << 3;

        int nthByte = ((x & (0xFF << nOffset) ) >> nOffset) & 0xFF;
        int mthByte = ((x & (0xFF << mOffset) ) >> mOffset) & 0xFF;

        int i = nthByte << mOffset;
        int j = mthByte << nOffset;

        // reset nTh & mthByte.
        x &= ~((0xFF << nOffset) | (0xFF << mOffset));

        // at last join the reseted value with mth and nTh bytes
        int ans = x | i | j;

        return ans;
}


int main(void) {
  printf("%0X\n", byteSwap(0x12345678, 1, 3));
  printf("%0X\n", byteSwap(0xDEADBEEF, 0, 2));
  return 0;
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Consider the following code used to read a file. byte[] buffer = new byte[BUFFER_SIZE]; String str...
Consider the following code used to read a file. byte[] buffer = new byte[BUFFER_SIZE]; String str = ""; try { while (true) { int read = infile.read(buffer); if (read <= 0) { break; } str += new String(buffer, 0, read); } } catch (IOException ex) { return; } Consider if BUFFER_SIZE is set to 1000 and the the file is 2775 bytes long. How many times will infile.read() be called? What value will infile.read() return on the next to last...
Write one difference for items below a.     char versus Character b.     Source code versus Byte code c.    Complier error...
Write one difference for items below a.     char versus Character b.     Source code versus Byte code c.    Complier error versus Runtime error d.   char [ ] versus String [ ]
Write an assembly code the counts the number of accuracies of the byte AAh in memory...
Write an assembly code the counts the number of accuracies of the byte AAh in memory from address 120Ah to address 130Ah. You need to use a subroutine and call it 'COUNT' to do so. You also need to provide the count in BCD if it was less than 64h so that you need to include another subroutine called 'ToBCD' to do so. assembly 8086
PLEASE DO NOT CHANGE THE CODE BELOW! In this problem, we will implement an nth root...
PLEASE DO NOT CHANGE THE CODE BELOW! In this problem, we will implement an nth root finder. In particular, please fill in the findNthRoot(int number, int n, int precision) method in the class NthRootFinder. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should fill in the answer with decimal places (i.e. with input 41 and precision 5, we should return 41.00000.) You may...
In C++ In this assignment, we will implement a square root approximater and then an nth...
In C++ In this assignment, we will implement a square root approximater and then an nth root approximater. Recall that the nth root of x is the number when raised to the power n gives x. We can use the concepts of binary search to approximate the square root of a number, and we can continue this logic to approximate the nth square root. We're going to use the concepts of binary search to try and approximate ending the square...
Please code by C++ The required week2.cpp file code is below Please ask if you have...
Please code by C++ The required week2.cpp file code is below Please ask if you have any questions instruction: 1. Implement the assignment operator: operator=(Vehicle &) This should make the numWheels and numDoors equal to those of the object that are being passed in. It is similar to a copy constructor, only now you are not initializing memory. Don’t forget that the return type of the function should be Vehicle& so that you can write something like: veh1 = veh2...
In this C program, the function reverseStr reverses the nth word in a string. If n...
In this C program, the function reverseStr reverses the nth word in a string. If n is larger than the number of words in the string then the function returns 0, else return the function modifies the string and returns 1. Task: write the reverseStr function that follow the requirements below: 1. consistent with the prototype and code below 2. original string must be changed 3. DON'T use functions in string.h Tips: 1. string input to this function is non-empty,...
Code the following in C++ and make sure to include all three files required at the...
Code the following in C++ and make sure to include all three files required at the end. Hotdogs – The World’s Greatest Cost Effective Wholesome Nutritious Food Due to world food supply scarcity and the burgeoning populations of the world’s countries, your hot stand business is globalizing to satisfy the world’s desire for a cost effective wholesome nutritious food source. Market studies have shown that tens of billions of people are craving for the eponymous hotdog which will result in...
C code required /* * isGreater - if x > y then return 1, else return...
C code required /* * isGreater - if x > y then return 1, else return 0 * Example: isGreater(4,5) = 0, isGreater(5,4) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 24 * Rating: 3 */
Write the C code required to configure pin 0 on port B to be an input...
Write the C code required to configure pin 0 on port B to be an input with the pullup resistor enabled.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT