Re: circular shift array



On Jul 31, 8:10 am, "Bint" <b...@xxxxxxxx> wrote:
Hi,

What is a simple way to shift the elements in an array, circularly? Is
there a way to do it so that you don't need a separate storage array?

IE I want to shift array A{1,2,3,4,5,6,7,8} by 3 to the right. The result
would be B{6,7,8,1,2,3,4,5)

Thanks!
B

In addition, sometimes shifting might not be required, like shifting 8
times on your array gives the same result, so have some kind of logic
like the following,

int no_of_shifts;
no_of_shifts = shift%n;
if(no_of_shifts == 0) return; /* No need to shift */
else if (n <= no_of_shifts/2) right_shift(no_of_shifts);
else left_shift(no_of_shifts);

.



Relevant Pages

  • Re: bit shifts by multiple bytes
    ... like putting the bytes into a long or uint and shifting that. ... If I want to shift this: ... each byte in the low order bits of an array of 16 bit words. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Packages and returning errors
    ... > array intact. ... sub is_a_instance_method { ... my $class = shift; ... You need to fix the scope of $error by moving its declaration outside ...
    (comp.lang.perl.misc)
  • Re: Packages and returning errors
    ... The perldoc function guide says about shift (which is ... "Shifts the first value of the array off and returns it, ... If an array of values are passed to a sub, ... Back to my package (which I am currently thinking might be out of my depth, ...
    (comp.lang.perl.misc)
  • Re: MS SQL geek wants to jump ship, plz help on first Perl script
    ... Next step is the MEAT inside the loop. ... Then you don't understand shift(). ... automatically works on the @_ array (ie, ... # a lot of memory, coz it reads the whole arrary into memory first, ...
    (comp.lang.perl.misc)
  • Re: bit shifts by multiple bytes
    ... The last byte was added to the end, and then it was a matter of moving right to left across the array, using two bytes in a UInt16 at a time to shift. ... You have a function composed of two parts: how many whole bytes will you shift followed by how many remainder bits will you shift. ... I'd start by breaking your starting number into bytes and store each byte in the low order bits of an array of 16 bit words. ...
    (microsoft.public.dotnet.languages.csharp)