It took me little time to create the mask as suggested by Paul. But I created it as follows.
First I left shifted 1 as follows
1 << (sizeof(int)*8-k);
If I consider k to be 10 and INT size as 32 I will get following mask
00000000010000000000000000000000 ( 1 at 23 rd position 32 - 10 = 22 )
Then add it with -1 (0xffffffff)
00000000010000000000000000000000
+ 11111111111111111111111111111111
+++++++++++++++++++++++++++++++++++
00000000001111111111111111111111 --> required mask with first 10 bits set to
Anding with the result of arithmetic shift will give logical shift result.
Following is the C code
unsigned srl(unsigned x, int k) {
/* Perform shift arithmetically */
unsigned xsra = (int) x >> k;
int mask = (1 << (sizeof(int)*8-k)) + -1;
int result = xsra & mask;
}
And It works.