Efficient bitshifting an array of int?

Posted by nn on Stack Overflow See other posts from Stack Overflow or by nn
Published on 2010-05-05T14:04:54Z Indexed on 2010/05/05 14:08 UTC
Read the original article Hit count: 261

Filed under:
|
|
|
|

Hi,

To be on the same page, let's assume sizeof(int)=4 and sizeof(long)=8.

Given an array of integers, what would be an efficient method to bitshift the array to either the left or right?

I am contemplating an auxiliary variable such as a long, that will compute the bitshift for the first pair of elements (index 0 and 1) and set the first element (0). Continuing in this fashion the bitshift for elements (index 1 and 2) will be computer, and then index 1 will be set.

I think this is actually a fairly efficient method, but there are drawbacks. I cannot bitshift greater than 32 bits. I think using multiple auxiliary variables would work, but I'm envisioning recursion somewhere along the line.

© Stack Overflow or respective owner

Related posts about c

    Related posts about array