Question

In: Computer Science

Write a C function hwkOneA, that takes a long int x as well as two integers...

Write a C function hwkOneA, that takes a long int x as well as two integers n and m as arguments, and returns a long int. Here is the function declaration: long int hwkOneA (long int x, int n, int m); The function should swap nibble n and m of a long int x (64-bit integer). A nibble is a four-bit aggregation. For this problem, the index of the most significant nibble is 0, and the index of the least significant nibble is 15 (so 0 , =), loops, switches, function calls, macros, conditionals (if or ?:) You are allowed to use all bit level and logic operations, left and right shifts, addition and subtraction, equality and inequality tests, integer constants (<=255), INT_MIN and INT_MAX, and casting between data types. (20 points).

Solutions

Expert Solution

/*
L - Given long integer (64-bits)
So L will have 16 nibbles. We have to swap nth and mth nibble. 
Let n<m.
0th nibble is the most significant, i.e. counting starts from 
left hand side if we write on paper.
So there will be n-1 nibbles to the left of nth nibble.
Let X be the long int with all nibbles greater than or 
equal to n, set to binary 0.
Let N be the long int with all nibbles except nth, set to binary 0.
Let Y be the long int with all nibbles less than or equal to n && 
all nibbles greater than or equal to m, set to binary 0.
Let M be the long int with all nibbles except mth, set to binary 0.
Let Z be the long int with all nibbles less than or equal to m, set 
to binary 0.
So, L = X + N + Y + M + Z
*/
#include<stdio.h>
long int X, Y, Z, M, N;
long int hawkOne(long int, int, int);
int main() {
        long int L = 2006;
        int n = 3, m = 10;
        printf("After interchanging %dth and %dth nibble in %ld value: %ld", n, m, L, hawkOne(L, n, m));
        getch();
        return 0;
}
long int hawkOne(long int L, int n, int m) {
        int i, j;
        long int T;
        if (m<n) {                   //If m<n, interchange
                int temp;
                temp = n;
                n = m;
                m = temp;
        }
        if (m == n)
                return L;

        T = L;
        //Find X
        T = T >> (16 - n);
        X = T << (16 - n);

        T = L;
        //Find N
        T = T << (n - 1);
        N = T >> 15;

        T = L;
        //Find Y
        T = T >> (16 - m);
        Y = T << (16 - m + n - 1);

        T = L;
        //FInd M
        T = T << (m - 1);
        M = T >> 15;

        T = L;
        //Find Z
        T = T << (m + 1);
        Z = T >> (m + 1);

        //Interchanging of nth and mth nibble
        T = 0;
        T = M;
        M = N >> m;
        T << m;
        T >> n;
        N = T;

        return X + N + Y + M + Z;
}

Related Solutions

C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
C++ write a function called divideBy that takes two integers as its input and returns the...
C++ write a function called divideBy that takes two integers as its input and returns the remainder. If the divisor is 0, the function should return -1, else it should return the remainder to the calling function.
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
C++ Write a function called gen_dates() that generates random dates. It takes two arrays of integers...
C++ Write a function called gen_dates() that generates random dates. It takes two arrays of integers called months and days to store the month and day of each date generated, a constant array of 12 integers called num_of_days that specify the number of days of each of the 12 months and an integer called size that specifies how many dates to generate and randomly generates size dates, storing the generated months in months array and generated days in days array....
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as a parameter, put all prime numbers less than n into an array, and print out the array and the sum of the numbers in that array. You can use the isPrime function above to save time.
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
C Write a function that takes as input an array of int pointers, multiplies the ints...
C Write a function that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value after multiplying by -1. If...
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT