In: Computer Science
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).
/* 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; }