P
P
Pavel Lapin2017-09-26 10:06:21
C++ / C#
Pavel Lapin, 2017-09-26 10:06:21

Bitwise array shift?

There is an array like [FFFF, FFFF, ...., FFFF, 3FFF] , you need to get [7FFF, FFFF, ...., FFFF, 9FFF], that is, shift the entire array by one bit. As I understand it, similar tasks in C are solved by assembler inserts, because there it is possible to save the bit displaced by the shift. But I'm interested in any solution to this problem)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
longclaps, 2017-09-26
@lapinpavel

pseudocode

int mask=0;
for i in 0..len(arr)-1{
    int newmask = arr[i]&1==0 ? 0 :0x8000;//
    arr[i]=(arr[i]>>1)  | mask;
    mask = newmask; 
}

In the example, the error is: last element 0x3FFF -> 0x9FFF

A
alex_ak1, 2017-09-26
@alex_ak1

rem = 0;
for( i = len-1; i > 0; i++ )
{
rem = (arr[i] & 1);
arr[i] = (arr[i] >> 1) | (rem << 31);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question