Question

In: Computer Science

Fill in the code for the following C functions. Function srl performs a logical right shifting...

Fill in the code for the following C functions. Function srl performs a logical right shifting using arithmetic right shift (given by value xsra), followed by other operations not including right shifts or division. Function sra performs an arithmetic right shift using a logical right shift (given by value xsrl), followed by other operations not including right shift or division. you may use the computation 8*size of (int) to determine w, the number of bits in data type int. The shift amount k can change from 0 to w-1.

unsigned srl (unsigned x, int k){

unsigned xsra = (int) x>> k;

.

.

.

}

int sra (int x, int k){

int xsrl = (unsigned) x >> k;

.

.

.

}

Solutions

Expert Solution

Solution:

#include <stdio.h>

unsigned srl(unsigned x, int k)

{

/* arithmetic right shifting */

unsigned xsra = (int) x >> k;

int w = sizeof(unsigned) << 3;

unsigned mask = ~(0xff << (w - k));

return xsra & mask;

}

int sra(int x, int k)

{

/* logical right shifting */

int xsrl = (unsigned) x >> k;

int w = sizeof(int) << 3;

unsigned sign = !!((0x1 << (w - 1)) & x);

unsigned mask = (~sign + 1) << (w - k);

return xsrl | mask;

}

//driver funtion to test

int main(int argc, char **argv)

{

printf("%.8x\n", srl(0x1 << 16, 8));

printf("%.8x\n", srl(~0x0, 6));

printf("%.8x\n", srl(0x0, 6));

printf("\n");

printf("%.8x\n", sra(0x9 << 26, 8));

printf("%.8x\n", sra(~0x0, 6));

printf("%.8x\n", sra(0x0, 6));

return 0;

}

Output:

Please give thumbsup, if you like it. Thanks.


Related Solutions

C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: ---the code can be general (can be tested with any int main() test function)--- Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: //...
C++ Code each of the following functions RECURSIVELY  (each function should use only the parameter value, and...
C++ Code each of the following functions RECURSIVELY  (each function should use only the parameter value, and any local function variables you might need ..no variables are to be declared outside the functions). void upTo(int number) This function prints all values from 1 up to the number. upTo(5); should output: 1 2 3 4 5 HINT: the following functions involve separating a digit from a number. Use of the arithmetic operators % and / are helpful here. Remember a % 10...
C++ Directions: Follow the insttructions of implementation and then fill in the following code for QueueArray...
C++ Directions: Follow the insttructions of implementation and then fill in the following code for QueueArray class Both given below: 5.1 Implement QueueArray<DataType>::QueueArray(int maxNumber) 5.2 Implement QueueArray<DataType>::QueueArray(const QueueArray& other) 5.3 Implement QueueArray<DataType>::~QueueArray() 5.4 Implement void QueueArray<DataType>::enqueue(const DataType& newDataItem) throw (logic_error) 5.5 Implement DataType QueueArray<DataType>::dequeue() throw (logic_error) 5.6 Implement void void QueueArray<DataType>::clear() throw (logic_error) 5.7 Implement bool QueueArray<DataType>::isEmpty() const 5.8 Implement bool QueueArray<DataType>::isFull() const 5.9 Implement void QueueArray<DataType>::putFront(const DataType& newDataItem) throw (logic_error) 5.10 Implement DataType QueueArray<DataType>::getRear() throw (logic_error) 5.11 Implement int...
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2....
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2. The variable value1 has been initialized to 200000. Declare the variable longPtr to be a pointer to an object of type long. b) Assign the address of variable value1 to pointer variable longPtr. c) Display the value of the object pointed to by longPtr. d) Assign the value of the object pointed to by longPtr to variable value2. e) Display the value of value2....
Write code in your “main” function that performs the following: a) For each n є {10^3,...
Write code in your “main” function that performs the following: a) For each n є {10^3, 5x10^3, 10^4, 5x10^4, 10^5}, randomly generate 5 integer arrays of length n. b) Run each of the five comparison sorts you implemented in Step 1 on all the five arrays generated in Step 2.a and record the worst-case actual running time and number of comparisons performed among elements in the input array. #include<iostream> #include<cmath> using namespace std; void displayArray(int a[], int n) { cout<<"\nArray...
Part 1 Write a C++ program in which you define the following logical functions: 1) contradictory...
Part 1 Write a C++ program in which you define the following logical functions: 1) contradictory function (¬?) 2) logical and (? ∧ ?) 3) logical inclusive or (? ∨ ?) 4) implication function (? → ?) 5) biconditional (? ↔ ?) 6) logical exclusive or (? ⨁ ?) You can find the logical definitions for the above functions as given by Bertrand Russell in your text. You need to write a method (function) for each corresponding logical function. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT