Right now I am reading the book Computer Systems : Programmer Perspective.
One problem in the book says to perform a logical right shift on a signed integer, I can't figure out how to start on this.
The following is the actual question from the book:
Fill in code for the following C functions.
Function
srl
performs a logical right shift using an arithmetic right shift (given by valuexsra
), followed by other operations not including right shifts or division.Function
sra
performs an arithmetic right shift using a logical right shift (given by valuexsrl
), followed by other operations not including right shifts or division.You may use the computation
8*sizeof(int)
to determinew
, the number of bits in data typeint
. The shift amountk
can range from0
tow − 1
.unsigned srl(unsigned x, int k) { /* Perform shift arithmetically */ unsigned xsra = (int) x >> k; . . . } int sra(int x, int k) { /* Perform shift logically */ int xsrl = (unsigned) x >> k; . . . }
I hope you understand now the question.